在显示MVC 3剃须刀上传图像 [英] Displaying an uploaded image in MVC 3 Razor

查看:96
本文介绍了在显示MVC 3剃须刀上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,这个新手做得不对显示上传到服务器上的图像时:

Well, this newbie is doing something wrong when displaying images uploaded to the server:

模型:

public class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string ImageUrl { get; set; }
}   

控制器(上传 - 按[HttpPost]公众的ActionResult称为创建):

controller (upload - called by the [HttpPost] public ActionResult Create):

public void Upload(Person person)
{
    var image = WebImage.GetImageFromRequest();
    var filename = Path.GetFileName(image.FileName);
    var path = Path.Combine(Server.MapPath("~/App_Data/Uploads/Fotos"), filename);
    image.Save(path);
    person.ImageUrl = Url.Content(Path.Combine("~/App_Data/Uploads/Fotos", filename));
}

创建视图:

...
@using (Html.BeginForm("Create", "Person", FormMethod.Post, new { @encType = "multipart/form-data" }))
{
    ...
    @FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false, includeFormTag: false, uploadText: "image")
    ...
    <div>
        <input type="submit" value="Create" /> |
        @Html.ActionLink("Back", "Index")
    </div> 
}

到目前为止,一切都很好,图像被上传到文件夹和URL保存

So far, so good, the image is uploaded to the folder and the url is saved

现在,我要看到它的详细信息视图

Now, I want to see it in the Detail View

详细视图:

<div class="display-foto">
    <img src="@Url.Content(Server.MapPath(Model.ImageUrl))" alt="IMAGE" />                         
</div>

查看生成的code,似乎一切都还好吗:

Viewing the generated code, everything seems to be alright:

<img src="D:\Users\x\Documents\Visual Studio 2010\Projects\CMI_AD\CMI_AD\App_Data\Uploads\Fotos\_nofoto.jpg" alt="IMAGE" />

但事实是,屏幕上出现什么,除了文本形象。

But the fact is that nothing appears on the screen except the text "IMAGE".

我在做什么错了?

P.S。我试过没有使用Server.Mappath,使用相对地址〜\\ App_Data文件\\上传\\ Fotos_nofoto.jpg,结果是一样的。

P.S. I've tried without the Server.MapPath, using the relative address "~\App_Data\Uploads\Fotos_nofoto.jpg" and the result is the same.

---编辑---

@ kmcc049:我想你的建议创建一个帮手

@kmcc049: I've tried your suggestion creating a helper

public static class MyHelpers
{
    public static IHtmlString MyImage(this HtmlHelper htmlHelper, string url)
    {
        var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
        var img = new TagBuilder("img");
        img.Attributes["alt"] = "[IMAGE]";
        img.Attributes["src"] = UrlHelper.GenerateContentUrl(url, htmlHelper.ViewContext.HttpContext);
        return MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing));
    }
}

在视图中调用:

@Html.MyImage(Model.ImageUrl)

生成code是

<img alt="[IMAGE]" src="/App_Data/Uploads/Fotos/_nofoto.jpg" />

但结果是一样的:没有图片:(

but the result is the same: no image :(

--- ---求解

显然App_Data文件是不是一个好位置来保存上传的文件,因为访问它们会导致错误403 - 禁止。我将文件移动到〜/上传/照片和它的作品。

Apparently the App_Data is not a good location to save uploaded files because accessing them will result an Error 403 - Forbidden. I move the files to ~/Uploads/Fotos and it works.

推荐答案

这不是一个有效的HTTP路径。这是该文件夹的路径您的计算机上。

That's not a valid HTTP path. That's the path to the folder on your computer.

尝试 UrlHelper.GenerateContentUrl()方法,而不是
http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.generatecontenturl.aspx

这篇关于在显示MVC 3剃须刀上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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