我正在创建一个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