从控制器检索图像路径并传递给 HttpPostedFileBase 图像模型 ASP.Net MVC
Retrieving Image path from Controller and pass to HttpPostedFileBase Image Model ASP.Net MVC
我是 ASP.Net MVC 的新手,我正在处理数据库中的图像列。我的问题是如何从控制器检索图像路径并发送到 HttpPostedFileBase 图像模型?我的代码中出现错误,告诉我 Image = row.Image
cannot implicitly convert type 'string' to 'system.web.httppostedfilebase'
这是我的代码:
控制器
public ActionResult CategoryForm(int? Id, int CategoryGroupId)
{
if (Id != null)
{
model = (from row in db.Categories
where row.Id == Id
select new NewCategoryViewModel
{
Id = row.Id,
CategoryGroupId = CategoryGroupId,
Name = row.Name,
Description = row.Description,
Image = row.Image,
Status = row.Status,
isMain = row.isMain
}).Single();
}
return PartialView("_CategoryFormPartialView", model);
}
型号
public class NewCategoryViewModel
{
public int Id { get; set; }
public int CategoryGroupId { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public HttpPostedFileBase Image { get; set; }
[Required]
public bool Status { get; set; }
[Required]
[Display (Name = "Display as main category?")]
public bool isMain { get; set; }
}
谢谢你的帮助。
首先,我强烈建议您不要在数据库中存储图像。这是非常低效的,你的数据库的性能会急剧下降。我推荐的是将图像上传到服务器并将参考保存在您的数据库中。这是 class 的 link 我必须将图像上传到保存在日期结构文件夹 [YYYY/MM/DD].
中的服务器
此外,如果数据库返回的是字符串而不是字节数组,则可能是您保存的是文件名而不是文件。我建议阅读 this post 以确认您正在保存文件。
数据库中的图像不是 HttpPostedFileBase 类型。您需要转换文件。
如果您正在使用 SQL 服务器的图像字段,您需要使用字节数组并将图像转换为 MemoryStream。
型号:
public byte[] Image { get; set; }
控制器:
Image = ConvertToImage(row.Image),
private HttpPostedFileBase ConvertToImage(byte[] image)
{
System.IO.File.ReadAllBytes(image);
return(HttpPostedFileBase)new MemoryPostedFile(bytes);
}
我是 ASP.Net MVC 的新手,我正在处理数据库中的图像列。我的问题是如何从控制器检索图像路径并发送到 HttpPostedFileBase 图像模型?我的代码中出现错误,告诉我 Image = row.Image
cannot implicitly convert type 'string' to 'system.web.httppostedfilebase'
这是我的代码:
控制器
public ActionResult CategoryForm(int? Id, int CategoryGroupId)
{
if (Id != null)
{
model = (from row in db.Categories
where row.Id == Id
select new NewCategoryViewModel
{
Id = row.Id,
CategoryGroupId = CategoryGroupId,
Name = row.Name,
Description = row.Description,
Image = row.Image,
Status = row.Status,
isMain = row.isMain
}).Single();
}
return PartialView("_CategoryFormPartialView", model);
}
型号
public class NewCategoryViewModel
{
public int Id { get; set; }
public int CategoryGroupId { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public HttpPostedFileBase Image { get; set; }
[Required]
public bool Status { get; set; }
[Required]
[Display (Name = "Display as main category?")]
public bool isMain { get; set; }
}
谢谢你的帮助。
首先,我强烈建议您不要在数据库中存储图像。这是非常低效的,你的数据库的性能会急剧下降。我推荐的是将图像上传到服务器并将参考保存在您的数据库中。这是 class 的 link 我必须将图像上传到保存在日期结构文件夹 [YYYY/MM/DD].
中的服务器此外,如果数据库返回的是字符串而不是字节数组,则可能是您保存的是文件名而不是文件。我建议阅读 this post 以确认您正在保存文件。
数据库中的图像不是 HttpPostedFileBase 类型。您需要转换文件。
如果您正在使用 SQL 服务器的图像字段,您需要使用字节数组并将图像转换为 MemoryStream。
型号:
public byte[] Image { get; set; }
控制器:
Image = ConvertToImage(row.Image),
private HttpPostedFileBase ConvertToImage(byte[] image)
{
System.IO.File.ReadAllBytes(image);
return(HttpPostedFileBase)new MemoryPostedFile(bytes);
}