问题描述
我正在尝试将数据(图像)从表单上传到我的.NET App。它将正确保存到数据库的路径,但是图像未保存在“ wwwroot / images”下。感谢您的帮助。
public async Task<IActionResult> Create([Bind("Id,Brand,Model,Description,Price,ImageUrl,CategoryId")] Car car)
{
if (ModelState.IsValid)
{
string webrootPath = _hostEnvironment.WebrootPath;
var files = HttpContext.Request.Form.Files;
if (files.Count > 0)
{
string fileName = Guid.NewGuid().ToString();
var uploads = Path.Combine(webrootPath,"images");
var extenstion = Path.GetExtension(files[0].FileName);
var filePath = Path.Combine(uploads,fileName,extenstion);
car.ImageUrl = "images" + "\\" + fileName + extenstion;
using (var filesstreams = new FileStream(filePath,FileMode.Create))
{
files[0].copyTo(filesstreams);
}
}
_context.Add(car);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CategoryId"] = new SelectList(_context.Categories,"Id","Name",car.CategoryId);
return View(car);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)