我如何展示图像?

时间:2014-10-10 23:20:39

标签: c# asp.net-mvc

我在Asp.Net中上传图像并且我真的在数据库中保存图像但是当我想要查询选择图像它返回没有图像的图像来源我想要任何人都知道解决方案请帮助我。输入此处的代码这是控制器代码:

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);
} 

1 个答案:

答案 0 :(得分:0)

您将路径设置为

it.Image_Path = filename;

然而它在服务器上存储为

file.SaveAs(HttpContext.Server.MapPath("~/Images/")
                                      + file.FileName);

我希望你能看到它需要it.Image_Path = "~/Images/" + filename;。另外,确保人们可以通过IIS实际访问路径。