2016-11-22 71 views
0

我正在嘗試上傳文件。下面的代碼可以在我的本地機器上運行,或者在運行命令行運行dll的遠程服務器上運行,但是當我嘗試發佈到我的測試環境並在iis下運行時,它會失敗。上傳文件ASP核心IIS

<form method="post" asp-action="Upload" asp-controller="Prebook" enctype="multipart/form-data"> 
    <div class="form-inline"> 
     <div class="col-md-2"> 
      <div class="form-group"> 
       <input type="file" name="files" data-input= "false" multiple class="filestyle" data-buttonName="btn-primary"> 
      </div> 
     </div> 
     <div class="col-md-2"> 
      <div class="form-group"> 
       <input type="submit" value="Upload File" class="btn btn-primary" /> 
      </div> 
     </div> 
    </div> 
</form> 

控制器邏輯

[HttpPost] 
public async Task<IActionResult> Upload(ICollection<IFormFile> files) 
{ 
    if (await _contextPB.UploadRow.AnyAsync()) 

    {     
     Danger(string.Format("Please LOAD the existing containers before uploading another file"), true); 
     return View(); 
    } 
    int rowCount = 0; 
    var uploads = Path.Combine(_environment.WebRootPath, "uploads"); 
    var _viewModel = new UploadViewModel(); 

    foreach (var file in files) 
    { 

     using (var streamReader = System.IO.File.OpenText(Path.Combine(uploads, file.FileName))) 
     { 
      var line = streamReader.ReadLine(); 
      var columnNames = line.Split(new[] { ',' }); 
      if (!ValidateColumnNames(columnNames)) 
      { 
       Danger(string.Format("Invalid Column Name in Upload file"), true); 
       return View(_viewModel); 
      } 
      while (!streamReader.EndOfStream) 
      { 
       var data = line.Split(new[] { ',' }); 
       var uploadRow = new UploadRow();      
       // validation & assignment logic removed 
       try 
       { 
        _contextPB.Add(uploadRow); 
        rowCount++; 
       } 
       catch (Exception e) 
       { 
        Danger(string.Format("<b>{0},{1}</b> database error", uploadRow.Container_Id, e), true); 
       } 
       line = streamReader.ReadLine(); 

      } 
     } 
    } 
} 

回答

1

嘗試添加catch塊,看看是什麼錯誤。

我假設權限問題。

[HttpPost] 
public async Task<IActionResult> Upload(ICollection<IFormFile> files) 
{ 
    try 
    { 
     if (await _contextPB.UploadRow.AnyAsync()) 
     { 
      Danger(string.Format("Please LOAD the existing containers before uploading another file"), true); 
      return View(); 
     } 

     // your code 
    } 
    catch (Exception ex) 
    { 
     // what is the error? 
     // add a breakpoint to see 
     throw; 
    } 
}