XHR级别2与jQuery文件上传 [英] XHR Level2 with jQuery for file upload

查看:127
本文介绍了XHR级别2与jQuery文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以访问jQuery的阿贾克斯原始XHR对象? 问题是,新的XMLHtt prequest 2级规范提供XHR称为上传的子属性,但显然jQuery的没有它。我想继续使用jQuery的Ajax,但我不知道如何与目前的jQuery库合并新功能。

How can I access the raw XHR object from jQuery Ajax? The thing is, the new XMLHttpRequest Level 2 specification provides a sub-property of XHR called upload but apparently jQuery doesn't have it yet. I want to keep using jQuery Ajax but I don't know how to merge the new functionality with current jQuery library.

推荐答案

jQuery中的原始XHR对象被包裹在jqXhr对象的新版本,它不具有任何参考XHR新上传的属性和文档不是很清楚如何可以做到这一点。 的方式,我发现做到这一点,有一些额外的设置,以获得一个成功的jQuery Ajax的HTML5文件上传是:

In new versions of JQuery the raw xhr object is wrapped in jqXhr Object which doesn't have any reference to the new upload property of the xhr and in the documentation is not very clear how to do it either. the way I found to do this, with some extra settings to get a successful jquery-ajax-HTML5 file uploader was:

var formData = new FormData($('#myForm')[0]);
$.ajax({
    url: 'upload.php',
    type: 'POST',
    xhr: function() {
        myXhr = $.ajaxSettings.xhr();
        if(myXhr.upload){
            myXhr.upload.addEventListener('progress',progressHandlerFunction, false);
        }
        return myXhr;
    },
    data: formData,
    cache: false,
    contentType: false,
    processData: false
});

使用$ .ajaxSettings.xhr()我得到的origianal XHR,然后我测试是否具有这样的性质上传到绑定进度事件控制进度(HTML5?)吧。其他设置允许我通过jQuery AJAX发送的形式作为一个FORMDATA对象。

with $.ajaxSettings.xhr() I get the origianal xhr, then I test if it has the property upload to bind a progress event to control a progress(HTML5?) bar. The other settings allow me to send via jquery ajax the form as a FormData object.

这篇关于XHR级别2与jQuery文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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