ASP.NET:获取正确的URL到上载的图像 [英] ASP.NET: Get correct url to uploaded image

查看:66
本文介绍了ASP.NET:获取正确的URL到上载的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上传这样的图片:

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);
        }

然后

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

但是它给了我404:找不到.

But it gives me a 404: Not found.

我该怎么办?我可以做得更好吗?

What should I do? Can I do this better?

推荐答案

使用:

@Url.Content(ProfilePicturesUploadDir)

获取文件夹的URL.

这篇关于ASP.NET:获取正确的URL到上载的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆