ASP.NET MVC3剃刀:文件上传给零的文件数 [英] ASP.NET MVC3 RAZOR: File Upload gives zero as file count

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

问题描述

我要上传多个文件到使用MVC3剃刀Web服务器。我有以下的code。在控制器中,我得到零的文件数。如何纠正它得到被上传的文件的实际数量和获取内容?

 公共类MyFileController:控制器
{

    公众的ActionResult MyFileProcessActionTest()
    {
        返回查看();
    }

    [HttpPost]
    公众的ActionResult MyFileProcessActionTest(IEnumerable的< System.Web.HttpPostedFileBase>文件)
    {

        INT fileCount = files.Count< System.Web.HttpPostedFileBase>();
        返回RedirectToAction(指数);
    }
}
 

查看

  @ {
ViewBag.Title =MyFileProcessActionTest;
}

< H2> MyFileProcessActionTest< / H>

@using(Html.BeginForm())
{

<输入类型=文件名称=文件ID =文件1/>
<输入类型=文件名称=文件ID =文件2/>

<输入类型=提交/>

}
 

阅读:

  1. <一个href="http://stackoverflow.com/questions/2491230/binding-httppostedfilebase-using-ajax-beginform">Binding HttpPostedFileBase使用Ajax.BeginForm

  2. ASP.NET MVC上传下载文件 的http://www.mikesdotnetting.com/Article/125/ASP.NET-MVC-Uploading-and-Downloading-Files

  3. 我如何验证的文件的文件类型上传?

  4. MVC 3文件的上传和模型绑定

解决方案

修改表单以匹配以下

  @using(Html.BeginForm(行动,控制,FormMethod.Post,新{ENCTYPE =的multipart / form-data的})){
{

&LT;输入类型=文件名称=文件[0]ID =文件1/&GT;
&LT;输入类型=文件名称=文件[1]ID =文件2/&GT;
&LT;输入类型=文件名称=文件[2]ID =文件3/&GT;

&LT;输入类型=提交/&GT;

}
 

指数0,1,2将使ModelBinder的绑定到的IEnumerable 此外 MIME类型还必须指定时发布文件到服务器

I need to upload multiple files into web server using MVC3 with RAZOR. I have the following code. In the controller, I am getting zero as the file count. How to correct it to get the actual number of files being uploaded and to get the content?

public class MyFileController : Controller
{

    public ActionResult MyFileProcessActionTest()
    {
        return View();
    }

    [HttpPost]
    public ActionResult MyFileProcessActionTest(IEnumerable<System.Web.HttpPostedFileBase> files)
    {

        int fileCount = files.Count<System.Web.HttpPostedFileBase>();
        return RedirectToAction("Index");
    }
}

VIEW

@{
ViewBag.Title = "MyFileProcessActionTest";
}

<h2>MyFileProcessActionTest</h2>

@using (Html.BeginForm())
{

<input type="file" name="files" id="file1" />
<input type="file" name="files" id="file2" />

<input type="submit"  />

}

READING:

  1. Binding HttpPostedFileBase using Ajax.BeginForm

  2. ASP.NET MVC Uploading and Downloading Files http://www.mikesdotnetting.com/Article/125/ASP.NET-MVC-Uploading-and-Downloading-Files

  3. How do I Validate the File Type of a File Upload?

  4. MVC 3 file upload and model binding

解决方案

change your form to match the following

@using(Html.BeginForm("action","controller",FormMethod.Post,new{encType = "multipart/form-data"})){
{

<input type="file" name="files[0]" id="file1" />
<input type="file" name="files[1]" id="file2" />
<input type="file" name="files[2]" id="file3" />

<input type="submit"  />

}

indices 0,1,2 will allow modelbinder to bind to IEnumerable furthermore encType also has to be specified when posting files to the server

这篇关于ASP.NET MVC3剃刀:文件上传给零的文件数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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