【问题标题】:Django: 413 (request entity too large) error upon ajax upload of Formdata containing filesDjango:在 ajax 上传包含文件的 Formdata 时出现 413(请求实体太大)错误
【发布时间】:2015-09-22 05:32:39
【问题描述】:

我在 Django 管理员中工作,我已经更改了表单提交以创建一个 FormData 对象,然后使用 jquery 通过 ajax 上传。

当上传中没有太多大文件时,这很有效,但超过一定限制时,我会遇到 Nginx(可能还有 uWSGI)错误,告诉我我的请求对象太大。显而易见的解决方案是将此属性设置得更高(我们被告知期望定期上传 500MB - 2GB),但团队对这会导致服务器端出现问题持谨慎态度,老实说,我不知道是否发送 1GB HttpRequest 是个坏主意。

如果提高 Nginx 和 uWSGI 属性没有不利之处,我们将继续这样做。如果有的话,我认为我的问题更多地是关于 Django/javascript,并且想知道分块此上传的最佳方式是什么(最好,我希望意味着必须重写最少数量的应用程序代码:))

感谢您的帮助! 罗伯特

【问题讨论】:

    标签: jquery ajax django nginx


    【解决方案1】:

    尝试使用Blob接口上传File对象,Blob.slice()如果Blob的初始size超过设定的字节长度,则将上传分成几部分

    var blob = new Blob(["0123456789"]);
    var len = blob.size;
    var arr = [];
    // if file size exceeds limit ,
    // `.slice()` file into two or more parts
    if (len > 5) {
      arr.push(blob.slice(0, 5), blob.slice(5));
    };
    
    arr.forEach(function(b, index) {
      setTimeout(function() {
        var reader = new FileReader();
        reader.onload = function(e) {
          // do stuff
          // e.g., send `e.target.result` , half of original file object to server
          // spaced two seconds apart
          // reassemble "slices" of `Blob` server side
          document.body.appendChild(
            document.createTextNode(
              e.target.result + "\n\n"
            )
          )
        }
        reader.readAsText(b);
      }, index * 2000);
    });

    【讨论】:

    猜你喜欢
    • 2021-12-28
    • 2017-02-16
    • 2020-12-29
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 2018-07-07
    • 2021-06-18
    • 2014-08-09
    相关资源
    最近更新 更多