PoeHaH
PoeHaH

Reputation: 1936

ASP.NET: Get correct url to uploaded image

I upload images like this:

private const string ProfilePicturesUploadDir = "~/_useruploads/ProfilePictures/";

var fileName = System.Guid.NewGuid()+Path.GetExtension(file.FileName);
file.SaveAs(Path.Combine(HttpContext.Current.Server.MapPath(ProfilePicturesUploadDir), fileName));

I want to retrieve the correct image in my view code. I am trying this:

 public static string GetProfilePictureUrl(string profilePicture)
        {
            return HttpContext.Current.Server.MapPath(ProfilePicturesUploadDir + profilePicture);
        }

and then

 <img src="@FileService.GetProfilePictureUrl(Model.ProfilePicture)" width="250" height="250" />

But it gives me a 404: Not found.

What should I do? Can I do this better?

Upvotes: 1

Views: 766

Answers (1)

SmartDev
SmartDev

Reputation: 2862

Use:

@Url.Content(ProfilePicturesUploadDir)

to get the URL of the folder.

Upvotes: 1

Related Questions