0

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?

6
  • Answer to above question can be seen here!. Thank you. Commented Mar 11, 2016 at 10:16
  • @mohit if you have deleted your answer, kindly delete this above comment as well and upvote the question if you think it has added to your knowledge or research. Thank you. Commented Mar 11, 2016 at 11:26
  • I had already upvoted it Commented Mar 11, 2016 at 11:46
  • @mohit For some strange reason, I can't see it here but thank you. :) Commented Mar 11, 2016 at 12:09
  • You had minus one point before now u have 0 Commented Mar 11, 2016 at 14:34

3 Answers 3

1

This isn't a URL, it's a file system path:

D:\Visual Studio Projects\HRMS\HRMS\App_Data\photos\5.png

The two are very different things. You might be able to get away with a file:// URL, maybe:

file://D/Visual Studio Projects/HRMS/HRMS/App_Data/photos/5.png

But it would make infinitely more sense to just use an actual URL. Something like:

/App_Data/photos/5.png

(Or an absolute URL, something relative to the current page, etc.)

So saving the path might look something more like this:

var path = VirtualPathUtility.ToAbsolute("~/App_Data/photos/" + fileName);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for replying. Sorry your suggestion isn't working. Image is still coming as broken and rendered HTML is <dd> <img src="/App_Data/photos/16.GIF" /> </dd>
0

Remove the Image from App_Data folder because it is a hidden Segment section and hence the application is denied access to that folder.

Just Create a New Folder outside the app_data folder.

You can now access the image in two ways

  1. Relative path Example: ../photos/5.png
  2. Absolute path Example: [http://localhost:portnumber/photos/5.png]

Helpful Tip:Once you run the application you can use the absolute path in the browser to see if the image is accessible. If you are not able to access it will tell u the physical path where it is trying to search that image. With this you will be easy able to fix the absolute path.

Comments

0

Replace your existing line-

TempData["filePath"] = fpath;

With

TempData["filePath"] = Url.Content("~/App_Data/photos/" + fileName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.