我正在創建一個JSP/Servlet Web應用程序,我想通過Ajax將文件上傳到Servlet。我會如何去做這件事?我正在使用jQuery。如何使用JSP/Servlet和Ajax將文件上傳到服務器?
我迄今所做的:
<form class="upload-box">
<input type="file" id="file" name="file1" />
<span id="upload-error" class="error" />
<input type="submit" id="upload-button" value="upload" />
</form>
有了這個jQuery的:
$(document).on("#upload-button", "click", function() {
$.ajax({
type: "POST",
url: "/Upload",
async: true,
data: $(".upload-box").serialize(),
contentType: "multipart/form-data",
processData: false,
success: function(msg) {
alert("File has been uploaded successfully");
},
error:function(msg) {
$("#upload-error").html("Couldn't upload file");
}
});
});
但是,它不會出現發送文件內容。
您可以使用XMLHttpRequest方法。 看看這個:http://stackoverflow.com/questions/6974684/how-to-send-formdata-objects-with-ajax-requests-in-jquery –
希望這會幫助你:[http:// www .webdeveloperjuice.com/2010/02/13/7-可信Ajax的文件上傳-插件-使用-的jquery /(http://www.webdeveloperjuice.com/2010/02/13/7-trusted-ajax -file-upload-plugins-using-jquery /) – gred