3

How to load image file from physical path with out create virtual directory? I use C# code behaind and image source is physical path? How tio convert C:\Folder\imageName.jpg to file:///C:/Folder/imageName.jpg

1 Answer 1

12

You need to use a controller action to serve that image:

public ActionResult MyImage()
{
    return File(@"C:\Folder\imageName.jpg", "image/jpg");
}

and in your view invoke this controller action to show the image:

<img src="@Url.Action("MyImage", "SomeController")" alt="myimage" />

The reason for this is because client browsers cannot access arbitrary files located on the server. If this image is not inside the virtual directory it cannot be referenced by a client. So it is the server that needs to expose it.

Sign up to request clarification or add additional context in comments.

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.