[英]Trying to upload a file using asp.net core
我正在从部分共享视图加载产品图像,我正在尝试使用上一个问题中建议的Ifrom文件。
所以在我的产品控制器中,我有以下方法
编辑
我只想在数据库中保存文件的路径,然后将文件保存在磁盘上,什么是实现此目的的正确方法。
public ActionResult FileUpload(ProductImages model)
{
if (model.Image != null)
{
var uploads = Path.Combine(hostingEnvironment.WebRootPath, "uploads");
var filePath = Path.Combine(uploads, GetUniqueName(model.Image.FileName));
model.Image.CopyTo(new FileStream(filePath, FileMode.Create));
}
// to do : Return something
return RedirectToAction("Index", "Home");
}
我的产品图片类
public class ProductImages
{
[Key]
public int ProductImageId { get; set; }
public int ProductID { get; set; }
public string ProductImageTitle { get; set; }
public string ProductImageUploadUrl { get; set; }
public string ProductImageRealPath { get; set; }
public string ServerIpAddress { get; set; }
public string ProductImageAltTag { get; set; }
public int DisplayOrder { get; set; }
public IFormFile Image { set; get; }
}
我正在通过以下方式加载的我的共享布局。
<div class="tab-pane" id="images">
Product Images
@await Html.PartialAsync("_ProductPicture", Model)
</div>
这是我的共享视图的代码,我认为我声明IFormFile的方式做错了
@model solitude.models.ProductImages
<div class="form-group">
<form asp-action="FileUpload" enctype="multipart/form-data">
<input asp-for="ImageCaption" />
<input asp-for="ImageDescription" />
<input asp-for="MyImage" />
<input type="submit" />
</form>
</div>
我收到以下错误
AggregateException:发生一个或多个错误。 (属性'ProductImages.Image'是接口类型('IFormFile')。如果它是导航属性,则通过将其强制转换为映射的实体类型来手动配置此属性的关系,否则使用'[NotMapped ]属性或通过在'OnModelCreating'中使用'EntityTypeBuilder.Ignore'。)System.Threading.Tasks.Task.ThrowIfExceptional(bool includeTaskCanceledExceptions)
InvalidOperationException:属性'ProductImages.Image'具有接口类型('IFormFile')。 如果它是导航属性,则通过将其强制转换为映射的实体类型来手动配置该属性的关系,否则使用'[NotMapped]'属性或'OnModelCreating'中的'EntityTypeBuilder.Ignore'忽略该属性。
编辑2
[HttpPost]
public ActionResult FileUpload(ProductImageVm model)
{
if (model.Image != null)
{
var uploads = Path.Combine(hostingEnvironment.WebRootPath, "uploads");
var fileId = GetUniqueName(model.Image.FileName);
var filePath = Path.Combine(uploads, fileId);
model.Image.CopyTo(new FileStream(filePath, FileMode.Create));
// to do : Save the record in ProductImage table
var pi = new ProductImages { ProductID = model.ProductId };
pi.ProductImageTitle = model.Title;
pi.ProductImageRealPath = fileId; //Storing the fileId
_context.ProductImages.Add(pi);
_context.SaveChanges();
}
return RedirectToAction("Index", "Home");
}
现在我正在另一张表格中加载表格可能是问题所在
您不应在实体类中使用IFormFile
类型属性。 根据注释,您要将映像存储在磁盘中,并将映像的路径存储在表中。 因此,只需删除public IFormFile Image { set; get; }
public IFormFile Image { set; get; }
public IFormFile Image { set; get; }
实体类定义中的属性。
您可以在视图模型中使用IFormFile
,该模型用于在视图和操作方法之间传输数据。 因此,创建具有视图所需属性的视图模型
public class ProductImageVm
{
public int ProductId { set;get;}
public string Title { set;get;}
public IFormFile Image { set; get; }
//Add other properties AS NEEDED by your view
}
现在要上传产品56的图像,创建此视图模型的对象,将ProductId设置为56并在GET操作中发送到该视图。 在视图中,我们将productId保留在一个隐藏的表单字段中,当我们需要保存新的ProductImages
实体记录时,可以在HttpPost操作中稍后使用该字段。
public IActionResult UploadImage(int productId)
{
var vm = new ProductImageVm { ProductId=productId};
return View(vm);
}
和视图,它是我们视图模型的强类型
@model ProductImageVm
<form asp-action="FileUpload" asp-controller="Products" method="post"
enctype="multipart/form-data">
<input asp-for="Title" />
<input asp-for="ProductId" type="hidden" />
<input asp-for="Image" />
<input type="submit" />
</form>
现在,在HttpPost操作方法中,您将使用与参数相同的视图模型,读取Image
属性并将其保存到磁盘并将路径存储在表中。 您不需要存储完整路径。 您可以存储相对路径。 在下面的示例中,当保存ProductImages实体记录时,我将唯一文件ID(文件名)存储到ProductImageRealPath
属性值。
[HttpPost]
public ActionResult FileUpload(ProductImageVm model)
{
if (model.Image != null)
{
var uploads = Path.Combine(hostingEnvironment.WebRootPath, "uploads");
var fileId = GetUniqueName(model.Image.FileName);
var filePath = Path.Combine(uploads,fileId);
model.Image.CopyTo(new FileStream(filePath, FileMode.Create));
}
// to do : Save the record in ProductImage table
var pi=new ProductImages { ProductId = model.ProductId};
pi.ProductImageTitle = model.Title;
pi.ProductImageRealPath = fileId; //Storing the fileId
db.ProductImages.Add(pi);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.