jQuery FileUpload:Ajax在上传图片时冻结浏览器(jpg,png)

时间:2014-12-23 09:00:31

标签: jquery-file-upload

我使用jQuery Fileupload插件将文件(.pdf,.zip,.dll和.png / .jpg)从本地上传到服务器。上述所有文件类型都可以在主要浏览器Chrome / Firefox / IE上成功上传,但.png / .jpg除外。

当尝试在Chrome上上传.png / .jpg时,上传插件会冻结此浏览器10-15分钟,直到发送XHR对象的数据完成,即使图像的大小也很小(〜 90KB)。相比之下,在FF / IE上完成相同的过程仅需1-2秒。

我正在使用此插件的v5.9以及这些覆盖选项:

var maxFileSize = 500000000000000;
var resizeMaxWidth = 1920;
var resizeMaxHeight = 1200;
var maxChunkSize = 1073741824;
var maxNumberOfFiles = 1;

$('#fileupload').fileupload('option', {
    forceIframeTransport: true,
    maxFileSize: maxFileSize,
    resizeMaxWidth: resizeMaxWidth,
    resizeMaxHeight: resizeMaxHeight,
    maxChunkSize: maxChunkSize,
    xhrFields: {
        withCredentials: true
    },
    acceptFileTypes: regularExpression,
    autoUpload: autoUpload
});

我尝试设置async: true;以强制浏览器不冻结页面,但失败了。

当脚本运行到_onSend()事件并且无法返回“成功”回调时,我在Chrome上获得了冻结页面。

_onSend: function (e, data) {  
        var that = this,
            jqXHR,
            slot,
            pipe,
            options = that._getAJAXSettings(data),
            send = function (resolve, args) {
                that._sending += 1;
                jqXHR = jqXHR || (
                    (resolve !== false &&
                    that._trigger('send', e, options) !== false &&
                    (that._chunkedUpload(options) || $.ajax(options))) ||
                    that._getXHRPromise(false, options.context, args)
                ).success(function (result, textStatus, jqXHR) {
                    that._onDone(result, textStatus, jqXHR, options);
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    that._onFail(jqXHR, textStatus, errorThrown, options);
                }).always(function (jqXHRorResult, textStatus, jqXHRorError) {
                    that._sending -= 1;
                    that._onAlways(
                        jqXHRorResult,
                        textStatus,
                        jqXHRorError,
                        options
                    );
                    if (options.limitConcurrentUploads &&
                            options.limitConcurrentUploads > that._sending) {
                        // Start the next queued upload,
                        // that has not been aborted:
                        var nextSlot = that._slots.shift();
                        while (nextSlot) {
                            if (!nextSlot.isRejected()) {
                                nextSlot.resolve();
                                break;
                            }
                            nextSlot = that._slots.shift();
                        }
                    }
                });
                return jqXHR;
            };
        this._beforeSend(e, options);
        if (this.options.sequentialUploads ||
                (this.options.limitConcurrentUploads &&
                this.options.limitConcurrentUploads <= this._sending)) {
            if (this.options.limitConcurrentUploads > 1) {
                slot = $.Deferred();
                this._slots.push(slot);
                pipe = slot.pipe(send);
            } else {
                pipe = (this._sequence = this._sequence.pipe(send, send));
            }
            // Return the piped Promise object, enhanced with an abort method,
            // which is delegated to the jqXHR object of the current upload,
            // and jqXHR callbacks mapped to the equivalent Promise methods:
            pipe.abort = function () {
                var args = [undefined, 'abort', 'abort'];
                if (!jqXHR) {
                    if (slot) {
                        slot.rejectWith(args);
                    }
                    return send(false, args);
                }
                return jqXHR.abort();
            };
            return this._enhancePromise(pipe);
        }
        return send();
    },

你知道如何强制这个插件的Ajax在Chrome上运行,就像在Firefox / IE上一样吗?或任何解决此问题的提示?

谢谢!

干杯,

0 个答案:

没有答案