2017-01-31 97 views
1

我一直試圖讓這個工作,但無濟於事。多部分身體長度限制16384超出

我想要做的是使用JQuery AJAX上傳一組FormData圖像和附件。

我不斷收到錯誤:「多域主體長度限制16384超標」

我發現了另一個類似的問題在這裏SO: Multipart body length limit exceeded exception

如果有人在這裏可以幫助我或點我的方向,這將不勝感激。 。這是在我的身邊快到午夜了,我快要放棄:(

我使用ASP.NET 1.1的核心

這裏是我的javascript:

let data = new FormData(); 
    data.enctype = "multipart/form-data"; 
    let file = $("#imgItem_image-upload-file")[0].files[0]; 

    data.append("image|" + file.name, file); //Works fine if alone. 

    //Does not work, causes error on server side. 
    for (var i = 0; i < objItem.Attachments[0].length; i++) { 
     let attFile = objItem.Attachments[0][i].File; 
     console.log(attFile); 
     data.append("attachment|" + attFile.name, attFile); 
    } 

    data.append("Category", objItem.Category); 
    data.append("NewCategory", objItem.NewCategory); 
    data.append("Name", objItem.Name); 
    data.append("IdentificationType", objItem.IdentificationType); 
    data.append("SerialNumber", objItem.SerialNumber); 
    data.append("IMEI", objItem.IMEI); 
    data.append("EngineNumber", objItem.EngineNumber); 
    data.append("MASNumber", objItem.MASNumber); 
    data.append("NumberPlate", objItem.NumberPlate); 
    data.append("VINNumber", objItem.VINNumber); 
    data.append("Description", objItem.Description); 
    $.ajax({ 
     url: "http://localhost:7001/api/AddPersonalItem", 
     type: "POST", 
     data: data, 
     //dataType: "json", 
     //headers: { 'Content-Type': false }, 
     //contentType: false, 
     contentType: false, //'multipart/form-data' 
     processData: false, 
     // headers: { 
     //  'Accept': 'application/json', 
     //  'Content-Type': 'application/json' 
     // }, 
     success: function (response) { 

     }, 
     error: function(jqXHR, textStatus, errorThrown) { 
      console.log(textStatus, errorThrown); 
     } 
    }); 

我還添加了這對我的Startup.js文件:

public void ConfigureServices(IServiceCollection services) 
    { 
     // Add framework services. 
     services.AddMvc(); 

     //Multipart 
     services.Configure<FormOptions>(x => 
     { 
      x.MultipartBodyLengthLimit = 60000000; 
     }); 
    } 

這裏是我的API控制器的代碼:

 public ServiceCallResponse AddPersonalItem() 
     { 
      ItemObject io = new ItemObject(); 
      io.Attachments = new List<Attachment>(); 
      io.Image = new Image(); 

      //Get files. 
      foreach (IFormFile file in Request.Form.Files) 
      { 
       //The file name also states what type of object this is. 
       string type = file.Name.Split('|')[0]; 
       string name = file.Name.Split('|')[1]; 

       StreamReader reader = new StreamReader(file.OpenReadStream()); 
       byte[] bytes = Encoding.Unicode.GetBytes(reader.ReadToEnd()); 
       string base64 = Convert.ToBase64String(bytes); 

       switch (type.ToLower().Trim()) 
       { 
        case "attachment": 
         Attachment a = new Attachment(); 
         a.Name = name; 
         a.Base64 = base64; 

         io.Attachments.Add(a); 
         break; 
        case "image": 
         io.Image.Name = name; 
         io.Image.Base64 = base64; 
         break; 
       } 
      } 
     } 

即使增加多部分體長後,我仍然得到完全相同的錯誤。

出現的錯誤上:

foreach (IFormFile file in Request.Form.Files)

如果我不夠清晰n這個,請你和我會盡力ellaborate! :)

+1

我真的很想在這個上使用.NET Core,但我開始認爲我需要切換到我的服務WCF ... 22天后,並計數... –

+0

https://github.com/ aspnet/Mvc/issues/5128 –

+1

所以我最終放棄了.NET Core,並在ASP.NET API 2中這樣做。我仍然沒有想出如何讓.NET Core允許比這個默認值更大的文件。我試過的所有鏈接都不起作用。API 2工作100% - 我現在只需要一個Windows主機環境... –

回答

0

您還需要增加值長度限制。默認值是4MB。

   services.Configure<FormOptions>(options => 
       { 
        options.ValueCountLimit = 10; //default 1024 
        options.ValueLengthLimit = int.MaxValue; //not recommended value 
        options.MultipartBodyLengthLimit = long.MaxValue; //not recommended value 
       }); 
1

在我的情況下,解決方案是增加MemoryBufferThreshold。

services.Configure<FormOptions>(options => 
{ 
    options.MemoryBufferThreshold = Int32.MaxValue; 
}); 
0

我得到的例外,我發現我的客戶有一個不正確的HTTP頭「內涵式」。 使用捲曲與不正確的輸入型捲曲試圖發送整個文件在帖子正文文本,不是一個很好的舉措:-)

例與 捲曲

: 這失敗

curl --request POST \ 
    --url https://stuff.net/api/storage \ 
    --header 'authorization: bearer MY_ACCESS_TOKEN' \ 
    --header 'cache-control: no-cache' \ 
    --header 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \ 
    --form MyfileName=b72871d79c81 \ 
    --form [email protected] \ 
    --form MyfileId=9401c94db46c 

工作

curl --request POST \ 
    --url https://stuff.net/api/storage \ 
    --header 'authorization: bearer MY_ACCESS_TOKEN' \ 
    --header 'cache-control: no-cache' \ 
    --form MyfileName=b72871d79c81 \ 
    --form [email protected] \ 
    --form MyfileId=9401c94db46c