使用 jQuery 进行分段文件上传

Multipart file upload using jQuery

有什么方法可以使用 jQuery in JSP + java 上传多部分文件吗? 在 jQuery AJAX.

中执行时出现错误
$.ajax({
    type: "POST",
    url: "${pageContext.request.contextPath}/users/imageUpload/" + $imageUpload + "/" + $userId‌​,
    contentType: "multipart/form-data",
    processData: false,
}).done(function(data) {}).fail(function(data) {
    alert("Ooopss..! Something Bad Happened.!");
});
$("input:file").change(function(objEvent) {
    var objFormData = new FormData();
    // GET FILE OBJECT 
    var objFile = $(this)[0].files[0];
    // APPEND FILE TO POST DATA
    objFormData.append('userfile', objFile);
    $.ajax({
        url: 'Here Your Server-Url'
        type: 'POST',
        contentType: false,
        data: objFormData,
        //JQUERY CONVERT THE FILES ARRAYS INTO STRINGS.SO processData:false
        processData: false,
        success: function(data) {}
    });
});

试试这个:

var data = new FormData(document.getElementById("yourFormID")); // your form ID
var url = jQuery("#yourFormID").attr("action"); // your form action 
$.ajax({
    url: url,
    type: "POST",
    data: data, // this will get all the input fields of your form.
    enctype: 'multipart/form-data',
    processData: false,  // tell jQuery not to process the data
    contentType: false,   // tell jQuery not to set contentType
    dataType: 'json', // as you want
    success: function(response) {        
        // success 
    }
});  // JQUERY Native Ajax End