文件上传MVC [英] File upload MVC

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

问题描述

通过在我看来,下面的标记:

With the following markup in my view:

<form action="Categories/Upload" enctype="multipart/form-data" method="post">
    <input type="file" name="Image">
    <input type="submit" value"Save">
</form>

在我的控制器:

public ActionResult Upload(FormCollection form)
{
    var file = form["Image"];
}

文件的值为
如果我使用的是不同的控制器控制器尝试在不同的看法,它与同code工作。

The value of file is null. If I try it in a different view using a different controller Controller and it works with the same code.

我在Vista上,MVC 1.0。VS2008

I have VS2008 on Vista, MVC 1.0.

为什么?

马尔科姆

推荐答案

使用 HttpPostedFileBase 作为您的操作参数。此外,添加了 AcceptVerb 属性设置为 POST

Use HttpPostedFileBase as a parameter on your action. Also, add the AcceptVerb attribute is set to POST.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload(HttpPostedFileBase image)
{
    if ( image != null ) {
        // do something
    }
    return View();
}

这code是相当的精神/ ASP.NET MVC的设计。

This code is quite in the spirit/design of ASP.NET MVC.

这篇关于文件上传MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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