我如何显示图像?
问题描述:
我在Asp.Net中上传图像,我真的保存在数据库中的图像,但是当我想要查询选择图像它返回图像的来源,没有图像,请我想任何人都知道解决方案请在此帮助Me.`enter码这是控制器代码:我如何显示图像?
public ActionResult UpLoadImage(HttpPostedFileBase file)
{
if (file != null)
{
UpLoadDBEntities context = new UpLoadDBEntities();
file = Request.Files[0];
string filename = file.FileName;
string contenttype = file.ContentType;
string full = filename + " " + contenttype;
//string path = System.IO.Path.Combine(
// Server.MapPath("~/images/") + file.FileName);
file.SaveAs(HttpContext.Server.MapPath("~/Images/")
+ file.FileName);
Image_table it = new Image_table();
it.Image_Path = filename;
context.Image_table.Add(it);
context.SaveChanges();
}
return View();
}
public JsonResult ShowImage()
{
UpLoadDBEntities context = new UpLoadDBEntities();
Image_table it = new Image_table();
var showimage = (from itbl in context.Image_table
where itbl.ID == 8
select new { itbl.Image_Path });
return Json(showimage , JsonRequestBehavior.AllowGet);
}
答
您设置路径
it.Image_Path = filename;
然而,它在服务器上得到店作为
file.SaveAs(HttpContext.Server.MapPath("~/Images/")
+ file.FileName);
我希望你能看到它需要是it.Image_Path = "~/Images/" + filename;
。另外,确保人们可以通过IIS实际访问路径。
我认为你正在寻找http://stackoverflow.com/questions/186062/can-an-asp-net-mvc-controller-return-an-image – 2014-10-10 23:34:27
为什么每个工作都有一个大写字母,我发现它很难阅读! – DavidG 2014-10-11 00:27:26