ASP.NET: Get correct url to uploaded image

时间秒杀一切 提交于 2021-02-10 23:52:53

问题


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?


回答1:


Use:

@Url.Content(ProfilePicturesUploadDir)

to get the URL of the folder.



来源:https://stackoverflow.com/questions/27395829/asp-net-get-correct-url-to-uploaded-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!