jQuery异步上传表单(包含文件)

这段代码用得太顺手了,惋惜忘了出处(应该是stackoverflow上面)。在此感谢原做者。javascript

var formData = new FormData($('form')[0]);
$.ajax({
    url: 'URL',  // Server script to process data
    type: 'POST',
    xhr: function() {
        // Custom XMLHttpRequest
        var myXhr = $.ajaxSettings.xhr()
        // Check if upload property exists
        if (myXhr.upload) {
            // For handling the progress of the upload
            myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
        }
        return myXhr;
    },
    // Ajax events
    beforeSend: function() {},
    success: function (data) {},
    error: function (err) {},
    // Form data
    data: formData,
    // Options to tell jQuery not to process data or worry about content-type
    cache: false,
    contentType: false,
    processData: false
});
相关文章
相关标签/搜索