2016-05-09 79 views
0

我是MVC的新手,我遇到了一個我似乎無法解決的問題。我已經看到過幾篇關於類似問題的文章,但沒有什麼能夠讓我滿足我需要實現的法案。在MVC中處理多個多文件輸入

我有一個MVC4項目,我需要在一個頁面上有多個多文件輸入,我需要能夠區分通過哪個輸入提交哪些文件。

我見過this SO文章建議有多個Post動作參數,但我的代碼似乎將它們全部作爲單個列表處理。

這裏是我的控制器代碼:

public ActionResult ProjectDocuments(C4Tbl_UploadedFiles c4tbl_uploadedfiles, IEnumerable<HttpPostedFileBase> File1, IEnumerable<HttpPostedFileBase> File2) 
    { 
     try 
     { 
      foreach (var file in File1) 
      { 
       if (file.ContentLength > 0) 
       { 
        //Handle the first file list 
       } 
      } 
      foreach (var file in File2) 
      { 
       if (file.ContentLength > 0) 
       { 
        //Handle the second file list 
       } 
      } 

這裏是我查看代碼:

<table border="0" id="cssTable" class="nobg"> 
    <tr> 
     <th style="width: 100px; min-width: 100px; max-width: 100px"> 
      <b>Type</b> 
     </th> 
     <th style="width: 400px; min-width: 400px; max-width: 400px"> 
      <b>File to Upload</b> 
     </th> 
     <th style="width: 500px; min-width: 500px; max-width: 500px"> 
      <b>Status</b> 
     </th> 
    </tr> 
    <tr> 
     <td style="width: 100px; min-width: 100px; max-width: 100px"> 
      <b>Blueprint(s)</b> 
     </td> 
     <td style="width: 400px; min-width: 400px; max-width: 400px"> 
      <input type="file" name="File1" id="BP" multiple style="width: 380px"/> 
     </td> 
     <td style="width: 500px; min-width: 500px; max-width: 500px; text-align: left"> 
      @Html.RadioButton("Submitted", "Yes", false, new { @style = "width: 25px;", groupname="group1" }) Uploaded  
      @Html.RadioButton("Submitted", "No", false, new { @style = "width: 25px;", groupname="group1" }) Not Uploaded  
      @Html.RadioButton("Submitted", "N/A", false, new { @style = "width: 25px;", groupname="group1" }) Not Applicable 
     </td> 
    </tr> 
    <tr> 
     <td style="width: 100px; min-width: 100px; max-width: 100px"> 
      <b>Recovery Guide(s)</b> 
     </td> 
     <td style="width: 400px; min-width: 400px; max-width: 400px"> 
      <input type="file" name="File2" id="RG" multiple style="width: 380px"/> 
     </td> 
     <td style="width: 500px; min-width: 500px; max-width: 500px; text-align: left"> 
      @Html.RadioButton("Submitted", "Yes", false, new { @style = "width: 25px;", groupname="group2" }) Uploaded  
      @Html.RadioButton("Submitted", "No", false, new { @style = "width: 25px;", groupname="group2" }) Not Uploaded  
      @Html.RadioButton("Submitted", "N/A", false, new { @style = "width: 25px;", groupname="group2" }) Not Applicable 
     </td> 
     <td> 
      <input type="submit" value="Submit" name="submit" /> 
     </td> 
    </tr> 
</table> 

我需要能夠接受任何或所有輸入端已經提供的文件,我需要要知道我的哪些輸入是通過文件提交的,所以我知道它們是哪種類型的文件,所以我可以在我的表中創建相關的數據庫條目。

任何人都可以看到一個簡單的解決方案,並指出我在正確的方向嗎?

+1

的名字你控件是'File1'和'File2',所以你的參數需要'IEnumerable Fi le1,IEnumerable File'(注意 - 你第二組單選按鈕將被忽略,因爲你給它們的名字與第一組名相同) –

+0

謝謝Stephen,我改變了一些帖子的代碼讀得不好。我已經編輯了這個現在正確讀取。 – Nevski

+0

在你的編輯中該方法的簽名現在是正確的,你可以遍歷File1和File2中的文件foreach(File1中的var文件){...' –

回答

0

感謝您的回答。我已經將此標記爲當前編輯的帖子作品,儘管我確信有更有效的方法來完成此操作。

這是工作控制器代碼:

public ActionResult ProjectDocuments(C4Tbl_UploadedFiles c4tbl_uploadedfiles, IEnumerable<HttpPostedFileBase> File1, IEnumerable<HttpPostedFileBase> File2) 
{ 
    try 
    { 
     foreach (var file in File1) 
     { 
      if (file.ContentLength > 0) 
      { 
       //Handle the first file list 
      } 
     } 
     foreach (var file in File2) 
     { 
      if (file.ContentLength > 0) 
      { 
       //Handle the second file list 
      } 
     } 

這是工作查看代碼:

<table border="0" id="cssTable" class="nobg"> 
<tr> 
    <th style="width: 100px; min-width: 100px; max-width: 100px"> 
     <b>Type</b> 
    </th> 
    <th style="width: 400px; min-width: 400px; max-width: 400px"> 
     <b>File to Upload</b> 
    </th> 
    <th style="width: 500px; min-width: 500px; max-width: 500px"> 
     <b>Status</b> 
    </th> 
</tr> 
<tr> 
    <td style="width: 100px; min-width: 100px; max-width: 100px"> 
     <b>Blueprint(s)</b> 
    </td> 
    <td style="width: 400px; min-width: 400px; max-width: 400px"> 
     <input type="file" name="File1" id="BP" multiple style="width: 380px"/> 
    </td> 
    <td style="width: 500px; min-width: 500px; max-width: 500px; text-align: left"> 
     @Html.RadioButton("Submitted", "Yes", false, new { @style = "width: 25px;", groupname="group1" }) Uploaded  
     @Html.RadioButton("Submitted", "No", false, new { @style = "width: 25px;", groupname="group1" }) Not Uploaded  
     @Html.RadioButton("Submitted", "N/A", false, new { @style = "width: 25px;", groupname="group1" }) Not Applicable 
    </td> 
</tr> 
<tr> 
    <td style="width: 100px; min-width: 100px; max-width: 100px"> 
     <b>Recovery Guide(s)</b> 
    </td> 
    <td style="width: 400px; min-width: 400px; max-width: 400px"> 
     <input type="file" name="File2" id="RG" multiple style="width: 380px"/> 
    </td> 
    <td style="width: 500px; min-width: 500px; max-width: 500px; text-align: left"> 
     @Html.RadioButton("Submitted", "Yes", false, new { @style = "width: 25px;", groupname="group2" }) Uploaded  
     @Html.RadioButton("Submitted", "No", false, new { @style = "width: 25px;", groupname="group2" }) Not Uploaded  
     @Html.RadioButton("Submitted", "N/A", false, new { @style = "width: 25px;", groupname="group2" }) Not Applicable 
    </td> 
    <td> 
     <input type="submit" value="Submit" name="submit" /> 
    </td> 
</tr> 

感謝所有幫助和建議

+0

你不是剛剛使用別人的代碼,而是自己回答了嗎?如果其他人花時間回答你,至少要給他尊重,以表明他的回答是正確的。 – webnoob

+0

我實際上使用了@StephenMuecke的輸入來編輯帖子中的代碼。代碼幾乎沒有改變,並且原始問題的參數中只有1個錯誤。我從來沒有使用別人的答案,並標記爲答案。 – Nevski

+0

在這種情況下,沒有問題:)一些新用戶並不真正知道SO如何工作,所以最好檢查一下。 – webnoob

-1

查看

@using (Html.BeginForm("Multiple", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { 
<table border="0" id="cssTable" class="nobg"> 
<tr> 
    <th style="width: 100px; min-width: 100px; max-width: 100px"> 
     <b>Type</b> 
    </th> 
    <th style="width: 400px; min-width: 400px; max-width: 400px"> 
     <b>File to Upload</b> 
    </th> 
    <th style="width: 500px; min-width: 500px; max-width: 500px"> 
     <b>Status</b> 
    </th> 
</tr> 
<tr> 
    <td style="width: 100px; min-width: 100px; max-width: 100px"> 
     <b>Blueprint(s)</b> 
    </td> 
    <td style="width: 400px; min-width: 400px; max-width: 400px"> 
     <input type="file" name="File1" id="BP" multiple style="width: 380px" /> 
    </td> 
    <td style="width: 500px; min-width: 500px; max-width: 500px; text-align: left"> 
     @Html.RadioButton("Submitted", "Yes", false, new { @style = "width: 25px;", groupname = "group1" }) Uploaded 
     @Html.RadioButton("Submitted", "No", false, new { @style = "width: 25px;", groupname = "group1" }) Not Uploaded 
     @Html.RadioButton("Submitted", "N/A", false, new { @style = "width: 25px;", groupname = "group1" }) Not Applicable 
    </td> 
</tr> 
<tr> 
    <td style="width: 100px; min-width: 100px; max-width: 100px"> 
     <b>Recovery Guide(s)</b> 
    </td> 
    <td style="width: 400px; min-width: 400px; max-width: 400px"> 
     <input type="file" name="File2" id="RG" multiple style="width: 380px" /> 
    </td> 
    <td style="width: 500px; min-width: 500px; max-width: 500px; text-align: left"> 
     @Html.RadioButton("Submitted", "Yes", false, new { @style = "width: 25px;", groupname = "group2" }) Uploaded 
     @Html.RadioButton("Submitted", "No", false, new { @style = "width: 25px;", groupname = "group2" }) Not Uploaded 
     @Html.RadioButton("Submitted", "N/A", false, new { @style = "width: 25px;", groupname = "group2" }) Not Applicable 
    </td> 
    <td> 
     <input type="submit" value="Submit" name="submit" /> 
    </td> 
</tr> 

}

控制器

[HttpPost] 
    public ActionResult Multiple(IEnumerable<HttpPostedFileBase> File1, IEnumerable<HttpPostedFileBase> File2) 
    { 
     try 
     { 
      foreach (var file in File1) 
      { 
       if (file.ContentLength > 0) 
       { 
        //Handle the first file list 
       } 
      } 
      foreach (var file in File2) 
      { 
       if (file.ContentLength > 0) 
       { 
        //Handle the second file list 
       } 
      } 

     } 
     catch (Exception ex) 
     { 

     } 
     return View(); 
    } 

請查看輸出,你可以在你的行動上傳的文件。 enter image description here

+0

感謝您的回覆。我不明白這將如何區分通過多個輸入提交的文件?我錯過了明顯的東西嗎?對不起,如果這是一個愚蠢的問題! – Nevski

+1

使用此解決方案,無法知道哪個輸入將文件提交給控制器。另外,jQuery fileupload在這裏完全沒有必要。 – Dygestor

0

文件上傳到在Request.Files收集服務器,其 一個的每個由默認 MVC模式粘合劑映射到HttpPostedFileBase對象。因此,負責 處理上載文件的控制器操作需要一個參數,該參數表示一個HttpPostedFileBase對象集合 。

public ActionResult ProjectDocuments() 
{ 
    HttpPostedFileBase upPic = Request.Files["File1"]; 
    if (upPic != null && upPic.ContentLength != 0 && upPic.InputStream != null) 
    { 
     //Handle the first file list 
    } 
    upPic = Request.Files["File2"]; 
    if (upPic != null && upPic.ContentLength != 0 && upPic.InputStream != null) 
    { 
    //Handle the first file list 
    } 
    return View(); 
} 

多種文件上傳。

public ActionResult ProjectDocuments(IEnumerable<HttpPostedFileBase> files) 
{ 
    foreach (HttpPostedFileBase file in files) 
    { 
    if (file != null && file .ContentLength != 0 && file .InputStream != null) 
    { 
    //Handle the first file list 
    } 
    } 
    return View(); 
} 
+1

感謝您的回覆。我想過使用這個,但是由於每個輸入都是針對多個文件的,我不認爲這是一個合適的解決方案? – Nevski

+0

「試試這個」不是一個答案。請[編輯]你的回答,以解釋在其中使用的原則,以及你的改變如何幫助OP。 – CodeCaster