2017-08-10 32 views
0

在這裏我有一個表單,其中我有一個輸入類型的文件上傳我的文件,當點擊上傳按鈕時,我需要發佈multipart/form-data到web api 其中我上傳文件到Minio Server.I已粘貼的JavaScript和Web API我用下面。試圖POST多部分/形式的數據通過javascript到web api

當我按下上傳按鈕後,我得到500(內部服務器錯誤)。幫我建議。

HTML that contains the input type file and upload button

$("#upload").click(function() { 
      var file = new FormData($('#uploadform')[0]); 
      file.append('tax_file', $('input[type=file]')[0].files[0]); 
      $.ajax({ 
       type: "POST", 
       url: 'http://localhost:53094/api/values', 
       data: file, 
       //use contentType, processData for sure. 
       contentType: "multipart/form-data", 
       processData: false, 
       beforeSend: function() {}, 
       success: function (msg) { 
        $(".modal .ajax_data").html("<pre>" + msg + 
         "</pre>"); 
        $('#close').hide(); 
       }, 
       error: function() { 
        $(".modal .ajax_data").html(
         "<pre>Sorry! Couldn't process your request.</pre>" 
        ); 
        $('#done').hide(); 
       } 
      }); 
     }); 

[HttpPost] 
    public string Post(IFormFile file) 
    { 
     try 
      { 
       var stream = file.OpenReadStream(); 
       var name = file.FileName; 
       minio.PutObjectAsync("student-maarklist", "sample.jpeg", stream, file.Length); 
       return "Success"; 
      } 
      catch (Exception ex) 
      { 
       return ex.Message; 
      } 
    } 

回答

0

我想你需要不提本地主機只是文件路徑就可以了。或將其替換爲本地主機的IP。

+0

但是對於post方法,我使用了相同的url。 –

0

對不起,我沒有一個錯誤我附加在JavaScript中的名稱不保存爲我在web api中給出的名稱。

我變了,

file.append('tax_file', $('input[type=file]')[0].files[0]); 

file.append('file', $('input[type=file]')[0].files[0]); 

和它的工作。