In an ASP.NET MVC application, file is successfully saved to a folder and its URL is saved to SQL database. URL is saved in an Absolute URL form i.e., (D:\Visual Studio Projects\HRMS\HRMS\App_Data\photos\5.png). Having problem in loading file in a browser from folder using this URL. Code implementation is:
[HttpPost]
[ActionName("UploadPhoto")]
public ActionResult UploadPhoto(HttpPostedFileBase photoPath)
{
var fileName = Path.GetFileName(photoPath.FileName);
if (photoPath.ContentLength > 0)
{
var path = Path.Combine(Server.MapPath("~/App_Data/photos"), fileName);
photoPath.SaveAs(path);
}
ViewBag.upload = "Success! Photo was uploaded successfully.";
string fpath = Path.Combine(Server.MapPath("~/App_Data/photos"), fileName);
TempData["filePath"] = fpath;
return RedirectToAction("CreateWithImage", new { path = fpath });
}
public ActionResult CreateWithImage(string path)
{
employee em = new employee();
em.districts = new SelectList(hc.districts, "name", "name");
string fp = Convert.ToString(TempData["filePath"]);
em.photoPath = fp;
return View(em);
}
file (image) is rendered in a view as:
@model HRMS.Models.employee
<dd>
<img src="@Url.Content(@Model.photoPath)" />
</dd>
When View is called, I see a broken link for the image. HTML(with correct file path) for the loaded page (View) is seen as:
<dd>
<img src="D:\Visual Studio Projects\HRMS\HRMS\App_Data\photos\5.png" />
</dd>
Can someone note the problem and guide accordingly?