1

I have an ImageUpload folder in the root of my ASP.NET MVC website, running on Windows and hosted on AWS.

When displaying the images, I need something like this:

<img src="/ImageUpload/MyImage.jpg" alt="">

When uploading images, I need a physical path...

string fullPath = System.IO.Path.Combine("C:\\MyWebsiteRoot\\ImageUpload",  MyImage.jpg);
file.SaveAs(fullPath);

I want to put the path in my web.Config:

<!-- if I use a url path, the save won't work -->
<add key="ImageUploadPath" value="/ImageUpload"></add>

<!-- if I use physical path, the display won't work -->
<add key="ImageUploadPath" value="C:\\MyWebsiteRoot\\ImageUpload"></add>

Is it possible to define ImageUpload path in the Web.Config in a manner that it can be used both as a physical path and url path? Or do I need to define 2 different variables in my Web.Config?

5
  • What is the question ? Commented Jul 29, 2018 at 6:00
  • did u try : <img src="~/ImageUpload/MyImage.jpg" alt=" "> ? with using , "~/" IIS express resolves the physical path Commented Jul 29, 2018 at 6:01
  • I want to define ImageUpload path in my WebConfg. How can I use it both as a physical path and url path? Commented Jul 29, 2018 at 6:01
  • You should not use the physical directory location for image src. just use the relative url like what you did. Commented Jul 29, 2018 at 6:02
  • @Shyju, thanks. so if I use: /ImageUpload, how would the file.SaveAs() would know where is the physical location? Commented Jul 29, 2018 at 6:05

1 Answer 1

1

You can just store your image folder (which is present in your app root) name in the app settings and use that for saving and rendering the images

<add key="ImageUploadPath" value="ImageUpload"></add>

and when you save the uploaded file, read this value and use that to build the path. You can create a string like "~/ImageUploadPath" and pass that to Server.MapPath method, which will return the physical file path corresponding to the virtual path you passed to it.

var fileName = "yourImageFileName.jpg";
var directory = ConfigurationManager.AppSettings["ImageUploadPath"];
var path = Path.Combine(Server.MapPath("~/" + directory), fileName);
model.Image.SaveAs(path);

When rendering the image, use the same approach to read the directory name and render the image src value.

For example,

public ActionResult Profile(int userId)
{
    var user = db.Users.Find(userId);
    var vm = new UserProfileVm { FirstName = user.FirstName , Id = user.Id };
    vm.ProfileImage = GetImagePath(vm.ImagePath);
    return View(vm);
}
private string GetImagePath(string imageName)
{
   return ConfigurationManager.AppSettings["ImageUploadPath"] + "/" + imageName;
}

and in your view,

@model UserProfileVm
<img src="~/@Model.ProfileImage" alt="@Model.FirstName" />
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.