0

I Make Upload Image In Asp.Net And I Really Save The Image In The Data Base But When i Want To Make A Query To Select The Image It Return The Source Of The Image With No Image Please I Want Any One Know The Solution Please Help Me.`enter code here This Is The Controller Code:

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);
} 
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Sci Amr
  • 11
  • 4

1 Answers1

0

You are setting the path as

it.Image_Path = filename;

Yet it gets store on the server as

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

I hope you can see it needs to be it.Image_Path = "~/Images/" + filename;. Also, make sure folks can actually access the path through IIS.

beautifulcoder
  • 10,832
  • 3
  • 19
  • 29