2016-02-25 30 views
0

我想從我的頁面上傳文件,但請求沒有收到張貼文件。請求未收到文件 - Asp.Net Mvc

我的表單是一個正常的Bootstrap模式,這是視圖。

@model InventarioWeb.Mvc.ViewModels.InventarioViewModel 

<!-- Modal --> 
<div id="ImportadorModal" class="modal fade" role="dialog"> 
    <div class="modal-dialog"> 

     <!-- Modal content--> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal">&times;</button> 
       <h4 class="modal-title">Importar Arquivo</h4> 
      </div> 
      <div class="modal-body"> 
       @using (Html.BeginForm("ImportarItensInventario", "Inventario", FormMethod.Post, new { enctype = "multipart/form-data" })) 
       { 
        <div class="row"> 
         <div class="col-md-10"> 
          @*<h4>Input Groups</h4>*@ 
          <div class="input-group"> 
           <span class="input-group-btn"> 
            <span class="btn btn-primary btn-file"> 
             Procurar&hellip; 
             <input type="file" 
               id="fileToUpload" 
               accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"> 
            </span> 
           </span> 
           <input type="text" class="form-control" readonly> 
          </div> 
          <span class="help-block"> 
           Selecione um arquivo 
          </span> 
         </div> 
         <div class="col-md-10"> 
          <input type="submit" id="SubmitArquivoInventario" name="Submit" value="Salvar Arquivo" class="btn btn-primary" disabled="disabled"/> 
         </div> 
        </div> 
        @*@Html.HiddenFor(x => x.InventarioId)*@ 
       } 
      </div> 
      <div class="modal-footer"> 
       <button type="button" class="btn btn-success" data-dismiss="modal">Close</button> 
      </div> 
     </div> 

    </div> 
</div> 

這是控制器的方法

[HttpPost] 
     public ActionResult ImportarItensInventario(Guid inventarioId) 
     { 
      if (Request.Files["UploadInventarioItems"] == null || Request.Files["UploadInventarioItems"].ContentLength == 0) 
      { 
       return RedirectToAction(nameof(Details), new { id = inventarioId }); 
      } 

      string path = $"{Server.MapPath("~/Uploads")}/{Request.Files["UploadInventarioItems"].FileName}"; 

      if (System.IO.File.Exists(path)) 
      { 
       System.IO.File.Delete(path); 
      } 

      Request.Files["UploadInventarioItems"].SaveAs(path); 

      var model = new InventarioViewModel {InventarioId = inventarioId}; 

      var result = _arquivoAppService.ImportarArquivo(InventarioViewModel.ToModel(model), Request.Files["UploadInventarioItems"].FileName); 

      return RedirectToAction(nameof(Details), new { id = inventarioId}); 
     } 

當我要求,id參數被接收,但我的文件不是。 此外,Request.Files沒有任何項目。

我在做什麼錯?

回答

1

添加名稱屬性爲您的輸入類型文件,您可以解決方法來獲取沒有此屬性的文件,但它更方便。

<input type="file" id="fileToUpload" name="upload" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"> 

,並在服務器上使用此方法來獲取文件:

if (Request.Files["upload"] == null || Request.Files["upload"].HasFile()) 
{ 
//do something 
} 

或類似這樣的多個文件:

foreach (string inputTagName in Request.Files) 
    { 
     if (!Request.Files[inputTagName ].HasFile()) continue; 
     //... continue processing 
} 
+0

它的工作原理!謝謝!! – Jedi31

+0

歡迎您:D – SilentTremor