2016-10-09 6 views
0

入力の名前に基づいてWebページ内の特定の要素/キーを参照する方法を知りたかったのです。IEnumerable <HttpPostedFileBase>の特定のキーを参照する方法

私は以下のようにフォームを持っていると私はSQL-Expressデータベースに三つの画像をアップロードしたいと思います:

else if (property.PropertyName == "Image1") 
        { 
         <div class="form-group"> 
          <label for="file1">Filename:</label> 
          <input type="file" name="fileUpload[0]" /> 
          @if (Model.Image1 == null) 
          { 
           <div class="form-control-static">No Image</div> 
          } 
          else 
          { 
           <div> Image</div> 
          } 
         </div> 
        } 
        else if (property.PropertyName == "Image2") 
        { 
         <div class="form-group"> 
          <label for="file2">Filename:</label> 
          <input type="file" name="fileUpload[1]" /> 
          @if (Model.Image2 == null) 
          { 
           <div class="form-control-static">No Image</div> 
          } 
          else 
          { 
           <div> Image</div> 
          } 
         </div> 
        } 
        else if (property.PropertyName == "Image3") 
        { 
         <div class="form-group"> 
          <label for="file3">Filename:</label> 
          <input type="file" name="fileUpload[2]" /> 
          @if (Model.Image3 == null) 
          { 
           <div class="form-control-static">No Image</div> 
          } 
          else 
          { 
           <div> Image</div> 
          } 
         </div> 
        } 

私のコントローラは以下の通りです:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Edit(Client client, IEnumerable<HttpPostedFileBase> fileUpload) 
    { 
     if (ModelState.IsValid) 
     { 
      if(fileUpload != null) 
      { 
       foreach (var file in fileUpload) 
       { 
        //if (file.ContentType == "jpg") 
        //{ 

         if (file.FileName == "fileUpload[0]") 
         { 
          client.Image1 = new byte[file.ContentLength]; 
          file.InputStream.Read(client.Image1, 0, file.ContentLength); 
         } 
         if (file.FileName == "fileUpload[1]") 
         { 
          client.Image2 = new byte[file.ContentLength]; 
          file.InputStream.Read(client.Image2, 0, file.ContentLength); 
         } 

         if (file.FileName == "fileUpload[2]") 
         { 
          client.Image3 = new byte[file.ContentLength]; 
          file.InputStream.Read(client.Image3, 0, file.ContentLength); 
         } 
        //} 
       } 
      }    
      Repo.SaveProduct(client);//this is an interface to save the changes to the database. 

      return RedirectToAction("Index"); 

私は戻ってマッピングするにはどうすればよいですEnumerableの特定の変数に、私はこのfile.FileName == "fileUpload [2]"を使用しています。私はそれが間違っていることをかなり確信しています。私はhtmlタグを参照しようとしています。これを行う方法はありますか?

+1

インデックスを使用あなたがここで何をしたいかは不明です。 –

答えて

0

リストに変換し、 `file.FileName`は、ユーザーがアップロードされたファイル(プロパティの名前ではなく)の名前を返します。

if(fileUpload != null) { 
    var files = fileUpload.ToList(); 
    for (var index = 0; index < files.Count; index++) { 
     var file = files[index]; 

     if (index == 0) { 
      client.Image1 = new byte[file.ContentLength]; 
      file.InputStream.Read(client.Image1, 0, file.ContentLength); 
     } 

     if (index == 1) { 
      client.Image2 = new byte[file.ContentLength]; 
      file.InputStream.Read(client.Image2, 0, file.ContentLength); 
     } 

     if (index == 2) { 
      client.Image3 = new byte[file.ContentLength]; 
      file.InputStream.Read(client.Image3, 0, file.ContentLength); 
     }     

    } 
} 
関連する問題