本文介绍了jQuery文件上传器只发一个块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django应用程序中使用了jQuery文件上传器。



我有一个问题,Django只收到一大块我的大文件。起初我以为这可能是我的 UploadFileHandler 的问题,但是当我从上传者登录 chunksend 事件时只是被解雇了一次(而不是我的案件中的9次)。



上传者只是发送一个块的任何想法?



这是我的代码:

  $('#fileupload')。fileupload({
dataType: 'json',
maxChunkSize:10000000,
add:function(e,data){

console.log(data);

var uploadRow = $('#upload-item-master')
.clone()
.removeAttr('id')
.appendTo(#upload-container)
。 hide()
.fadeIn(1000);
uploadRow.find('。name')。text(data.files [0] .name);

var jqXHR = data.submit()
.always(function(result,textStatus,jqXHR){
if(result.status == 201){
uploadRow .find('。progress .bar')。css('width','100%');
uploadRow.find('。progress')。removeClass('active');
} else {
uploadRow.find('。progress .bar')。css('width','100%');
uploadRow.find('。progress')。removeClass('progress-success');
uploadRow.find('。progress')。removeClass('progress-success');
uploadRow.find('。progress')。addClass('progress-danger');
}
}
},
chunksend:function(e,data){
console.log(Chunk sent);
},
progress:function(e,data){
var progress = parseInt(data.loaded / data.total * 100,10);

}
});


解决方案

解决方案是插件需要一个 JSON ok,其他一切都会被解释为不好,下一个块不发送。



使用此代码

  return HttpResponse(json.dumps({ 'status':201}),content_type =application / json)

插件发送块块到服务器。不幸的是,服务器 - 除了正确的标题 - 将它们解释为单独的文件,并为每个块创建一个新文件...我为新的问题创建了一个新问题。


I'm using the jQuery file uploader in my Django application.

I've got the problem that Django just receives one chunk of my big file. At first I thought that it might be an issue with my UploadFileHandler but when I log the chunksend event from the uploader it is just fired once (instead of 9 times in my case).

Any ideas why the uploader is just sending one chunk?

Here's my code:

$('#fileupload').fileupload({
            dataType: 'json',
            maxChunkSize: 10000000,
            add: function (e, data) {

                console.log(data);

                var uploadRow = $('#upload-item-master')
                                    .clone()
                                    .removeAttr('id')
                                    .appendTo("#upload-container")
                                    .hide()
                                    .fadeIn(1000);
                uploadRow.find('.name').text(data.files[0].name);

                var jqXHR = data.submit()
                        .always(function (result, textStatus, jqXHR) {
                             if(result.status == 201) {
                                uploadRow.find('.progress .bar').css('width','100%');
                                uploadRow.find('.progress').removeClass('active');
                             } else {
                                uploadRow.find('.progress .bar').css('width','100%');
                                uploadRow.find('.progress').removeClass('progress-success');
                                uploadRow.find('.progress').removeClass('progress-success');
                                uploadRow.find('.progress').addClass('progress-danger');
                             }
                        })
            },
            chunksend: function(e, data) {
                console.log("Chunk sent");
            },
            progress: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);

            }
        });
解决方案

The solution is that the plugin needs a JSON ok, everything else will be interpreted als something bad and the next chunk is not sent.

With this code

return HttpResponse(json.dumps({ 'status': 201 }), content_type="application/json")

the plugin sends chunk by chunk to the server. Unfortunately the server - beside correct headers - interprets them as seperate files and creates one new file per chunk... I've created a new question fo the new problem here.

这篇关于jQuery文件上传器只发一个块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:04