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
});
相關文章
相關標籤/搜索