2017-03-18 8 views
1

Word文書、Pdf、画像など複数のファイルをアップロードしたいと思います。 アップロードするファイルの数がわからないため、ファイルの入力を1つ使用する必要があります。複数のファイルを1つの入力でAsp MVCにアップロード

私のコードはこれですが問題はありますが、サーバー側にファイルを送信できません。

コントローラコード:

public ActionResult Create([Bind(Include = "Id,Content")] Message message, IEnumerable<HttpPostedFileBase> files) 
    { 

     if (ModelState.IsValid) 
     { 
     // Object save logic 
     SaveFiles(message,files); 
     return RedirectToAction("Index"); 
     } 
     return View(message); 
    } 

ビューのコードの一部:

<form name="registration" action=""> 
      @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
      <div class="form-group"> 
       <label for="Content" >Content:</label> 
       <textarea class="form-control compose_content" rows="5" id="Content" name="content"></textarea> 

        <div class="fileupload"> 
         Attachment : 

         <input id="files" name="files" type="file" multiple /> 
        </div> 
      </div> 
      <button type="submit" class="btn btn-default compose_btn" >Send Message</button> 
     </form> 

私は、ファイルを保存し、オブジェクトを保存して問題はありません。 問題のみ: ファイルリストはnullです。

+0

あなたの「

」要素を表示してください –

+0

私はそれを質問に追加しました。 –

答えて

2

ファイルをアップロードするには、フォームにenctype= multipart/form-data属性を含める必要があります。

<form name="registration" action="" enctype= "multipart/form-data"> 

またはより良い

@using (Html.BeginForm(actionName, controllerName, FormMethod.Post, new { enctype= "multipart/form-data" })) 

と私は強くあなたがビューにモデルを渡すと強くお使いのモデルの性質のために、あなたのhtmlを作成するために、HtmlHelper方法を入力した使用をお勧めします。

+0

私は[at]モデルIEnumerable を渡しました。私はデフォルトのaspコードに自分のコードを追加しました。私は[Html.BeginForm()]を使っていました。今すぐあなたのコードを追加します。 –

+0

ありがとうございました:enctype = "multipart/form-data"を追加しました。および:actionName、controllerName、FormMethod.Post、new {enctype = "multipart/form-data"}。すべて固定されています。ありがとうございました –

関連する問題