超过了多部分车身长度限制16384 [英] Multipart body length limit 16384 exceeded

查看:550
本文介绍了超过了多部分车身长度限制16384的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图使它起作用,但无济于事.

I've been trying to get this to work, but to no avail.

我想做的是使用JQuery AJAX上传一组FormData图像和附件.

What I am trying to do, is to upload a set of FormData images and attachments using JQuery AJAX.

我不断收到错误消息:超出了身体的多个部分的长度限制16384"

I keep getting the error: "Multipart body length limit 16384 exceeded"

我在这里找到了另一个类似的问题: 多主体长度限制超出例外

I found another similar question here on SO: Multipart body length limit exceeded exception

如果这里有人可以帮助我或为我指明方向,将不胜感激.在我这边快到午夜了,我要放弃:(.

If anyone here can help me out or point me in a direction, that would be greatly appreciated. It's almost midnight on my side, and I'm about to give up :(.

我正在使用ASP.NET Core 1.1.

I am using ASP.NET Core 1.1.

这是我的JavaScript:

Here is my 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文件中:

I also added this to my Startup.js file:

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

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

这是我的API控制器的代码:

And here is my API Controller's code:

        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;
                }
            }
        }

即使增加了多部分主体的长度,我仍然会得到完全相同的错误.

Even after increasing the multipart body length, i am still getting the exact same error.

错误发生在:

foreach(Request.Form.Files中的IFormFile文件)

foreach (IFormFile file in Request.Form.Files)

如果对此我还不够清楚,请询问,我会尽力而为! :)

If I am not clear enough n this, please ask and I'll try to ellaborate! :)

推荐答案

我遇到了异常,发现客户端的HTTP标头"content-type" 不正确. 使用错误输入类型 curl curl 尝试将整个文件作为文本发送到POST正文中,这不是一个好办法:-)

I got the exception and I found that my client had a incorrect HTTP header "content-type". Using curl with the incorrect input type curl tried to send the entire file in the POST Body as text, not a great move :-)

带有 curl 的示例: 此失败:

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 file=@nature-sky-twilight.jpg \
  --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 file=@nature-sky-twilight.jpg \
  --form MyfileId=9401c94db46c

这篇关于超过了多部分车身长度限制16384的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆