Asp.net mvc 2 如何在ASP.NET MVC2中处理文件输入

Asp.net mvc 2 如何在ASP.NET MVC2中处理文件输入,asp.net-mvc-2,file-upload,Asp.net Mvc 2,File Upload,由于没有文件输入的助手,我们如何安全地处理文件输入 如果只是有一个按钮更好的话 <input type="button" value="Upload File" /> 模型将不会用文件填充,因此我需要执行其他操作。。。只是不知道:( 非常感谢您的帮助 谢谢。输入应该有一个名称: <input type="file" name="File" /> 最后,在控制器操作中处理文件上载: [HttpPost] public void General(GeneralModel

由于没有文件输入的助手,我们如何安全地处理文件输入

如果只是有一个按钮更好的话

<input type="button" value="Upload File" />
模型将不会用文件填充,因此我需要执行其他操作。。。只是不知道:(

非常感谢您的帮助


谢谢。

输入应该有一个名称:

<input type="file" name="File" />
最后,在控制器操作中处理文件上载:

[HttpPost]
public void General(GeneralModel model)
{
    var file = model.File;
    if (file != null && file.ContentLength > 0)
    {
        // The user selected a file to upload => handle it
        var fileName = Path.GetFileName(file.FileName);
        var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), fileName);
        file.SaveAs(path);
    }
    return View(model);    
}

Phil Haack关于ASP.NET MVC中的文件上载。

谢谢,你刚刚忘记我需要
:)我想这很明显:-)文件上载总是需要
多部分/表单数据。我必须处理两个表单,对吗?一个用于图像,另一个用于其他属性…?不一定。一个表单可以处理文件上传和正常输入,只要您有正确的
enctype
。明白了,它可以处理一切:)非常感谢提醒。
<input type="file" name="File" />
public class GeneralModel
{
    // The name of the property corresponds to the name of the input
    public HttpPostedFileBase File { get; set; }
    ...
}
[HttpPost]
public void General(GeneralModel model)
{
    var file = model.File;
    if (file != null && file.ContentLength > 0)
    {
        // The user selected a file to upload => handle it
        var fileName = Path.GetFileName(file.FileName);
        var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), fileName);
        file.SaveAs(path);
    }
    return View(model);    
}