2012-02-23 10 views
0

RAZORを使用してMVC3を使用して複数のファイルをWebサーバーにアップロードする必要があります。私は次のコードを持っています。コントローラでは、ファイル数としてゼロになっています。アップロードするファイルの実際の数を取得してコンテンツを取得する方法を修正するにはどうすればよいですか?ASP.NET MVC3 RAZOR:ファイルアップロードでファイル数がゼロになる

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ファイルのアップロードとダウンロード 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

答えて

5
IEnumerableにバインドすることができますmodelbinder次

@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" /> 

} 

インデックス0,1,2に一致するようにフォームを変更することは

さらにencTypeまた、サーバーにファイルを投稿する際に指定する必要があります

+0

間違ったオーバーロードがあります。 – jgauffin

+0

thanx jgauffin。お返事を編集 –

+0

ありがとうございました。働いた。{ – Lijo

8

フォームファイルを含めるべきであることを示すためにformタグでenctype属性を含める必要があります。

@using (Html.BeginForm("YourAction", "Controller", FormMethod.Post, new {enctype="multipart/form-data")) 
{ 
} 
+1

非常に多くの人がファイルのアップロードを含むフォームのenctype = "multipart/form-data"を逃している – linquize

+1

私は 'GET'メソッドを使ってファイルを投稿できないと信じています –

+2

@MuhammadAdeelZahid: PUTとPOSTだけが動作します。 – jgauffin

関連する問題