Asp.net mvc 3 如何在MVC3中显示数据库中的图像并在视图中显示?
大家好,我有一个FileUpload控件,用户可以在其中上载图像和文档文件,我正在数据库中保存路径的Url 现在,当我从数据库检索数据时,我想检查文件是.doc还是图像如果它是doc,它将打开文件我的问题是如何为图像执行该操作我如何检查它是否是图像,我必须在图像控件中显示图像MVC3中从未对图像控件进行过操作 这是我的控制器代码Asp.net mvc 3 如何在MVC3中显示数据库中的图像并在视图中显示?,asp.net-mvc-3,image,Asp.net Mvc 3,Image,大家好,我有一个FileUpload控件,用户可以在其中上载图像和文档文件,我正在数据库中保存路径的Url 现在,当我从数据库检索数据时,我想检查文件是.doc还是图像如果它是doc,它将打开文件我的问题是如何为图像执行该操作我如何检查它是否是图像,我必须在图像控件中显示图像MVC3中从未对图像控件进行过操作 这是我的控制器代码 public ActionResult ViewAttachments(string AttachmentName) { try
public ActionResult ViewAttachments(string AttachmentName)
{
try
{
AttachmentName = Session["AttachmentUrl"].ToString();
var fs = System.IO.File.OpenRead(Server.MapPath(" "+ AttachmentName+" "));
return File(fs, "application/doc", AttachmentName);
}
catch
{
throw new HttpException(404, "Couldn't find " + AttachmentName);
}
}
我在这里使用的是aspx页面我必须使用的html和我必须更改的代码这里有任何建议请您可以做的是简单地检查扩展名,然后在执行时返回文件结果,或者返回包含简单图像控件的部分视图 要扩展代码,可以执行以下操作:
public ActionResult ViewAttachments(string AttachmentName)
{
try
{
AttachmentName = Session["AttachmentUrl"].ToString();
var fs = System.IO.File.OpenRead(Server.MapPath(" " + AttachmentName + " "));
var ext = Path.GetExtension(fs.Name);
switch (ext)
{
case "doc":
return File(fs, "application/doc", AttachmentName);
case "jpg":
case "jpeg":
case "png":
return PartialView("_imgControl", "http://www.mysite.com/downloads/" + AttachmentName + ext);
}
}
catch
{
throw new HttpException(404, "Couldn't find " + AttachmentName);
}
}
然后在局部视图中,您可以将模型对象(在本例中是站点上图像路径的url)返回到标准html图像控件