Django的:在阿贾克斯上传FORMDATA包含文件413(请求实体太大)错误 [英] Django: 413 (request entity too large) error upon ajax upload of Formdata containing files

查看:324
本文介绍了Django的:在阿贾克斯上传FORMDATA包含文件413(请求实体太大)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我Django管理中的工作,我已经改变了表单提交创建FORMDATA对象,然后通过AJAX上传与jQuery。

这伟大工程时,有没有上载了太多的大文件,但超过一定限度,我得到击中,Nginx的(也可能uWSGI)告诉我,我的请求对象是太大的误差。显而易见的解决方案是设置该属性更高(我们已经被告知要期待500MB的定期上传 - 2GB),但球队警惕在服务器端这会造成问题,老实说,我不知道是否未发送1GB HTT prequest是一个坏主意。

如果没有缺点加大了Nginx的和uWSGI属性,我们将继续前进,做到这一点。如果有,我想我的问题变得更加关于Django / JavaScript的,不知道什么该分块上传将是最好的方式(用最好的,我希望是指不必重写最少的应用code :))

感谢您的帮助! 罗伯特·

解决方案

尝试使用的 的Blob 接口上传文件对象的 Blob.slice() 以上传分成部分如果的Blob 的初始尺寸超过设定字节长度

VAR一滴=新的斑点([0123456789]); VAR的len = blob.size; VAR ARR = []; //如果文件大小超过限制, //`.slice()`文件分成两个或更多部分 如果(LEN→5){   arr.push(blob.slice(0,5),blob.slice(5)); }; arr.forEach(功能(B,索引){   的setTimeout(函数(){     VAR读卡器=新的FileReader();     reader.onload =功能(E){       //做的东西       //例如,发送'e.target.result`,半原始文件对象服务器       //间隔2秒开       //重组`Blob`服务器端的切片       document.body.appendChild(         document.createTextNode(           e.target.result +\ñ\ N         )       )     }     reader.readAsText(B);   },指数* 2000); });

I'm working within the Django admin, and I've changed the form submissions to create a FormData object and then upload via ajax with jquery.

This works great when there aren't too many large files in the upload, but over a certain limit, I get hit with an Nginx (and possibly also uWSGI) errors that tell me my request object is too large. The obvious solution is to set this attribute higher (we've been told to expect regular uploads of 500MB - 2GB) but the team is wary about this causing issues on the server side, and I honestly have no idea whether or not sending a 1GB HttpRequest is a bad idea.

If there's no downside to upping the Nginx and uWSGI attributes, we'll go ahead and do that. If there are, I think my question becomes more about Django/javascript, and wondering what the best way of chunking this upload would be (by best, I hopefully mean having to rewrite the least amount of application code :) )

Thanks for the help! Robert

解决方案

Try utilizing Blob interface to upload File objects , Blob.slice() to divide upload into parts if initial size of Blob exceeds set byte length

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

这篇关于Django的:在阿贾克斯上传FORMDATA包含文件413(请求实体太大)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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