如何使用 JSP/Servlet 和 Ajax 将文件上传到服务器? [英] How can I upload files to a server using JSP/Servlet and Ajax?
问题描述
我正在创建一个 JSP/Servlet Web 应用程序,我想通过 Ajax 将文件上传到 servlet.我该怎么做呢?我正在使用 jQuery.
I'm creating a JSP/Servlet web application and I'd like to upload a file to a servlet via Ajax. How would I go about doing this? I'm using jQuery.
到目前为止我已经完成了:
I've done so far:
<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:
With this 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");
}
});
});
但是,它似乎没有发送文件内容.
However, it doesn't appear to send the file contents.
推荐答案
就这一点而言,从 jQuery 使用的当前 XMLHttpRequest
版本 1 开始,不是可以通过 XMLHttpRequest
使用 JavaScript 上传文件.常见的解决方法是让 JavaScript 创建一个隐藏的 并将表单提交给它,以便创建异步发生的印象.这也正是大多数 jQuery 文件上传插件正在做的事情,例如 jQuery Form plugin (
To the point, as of the current XMLHttpRequest
version 1 as used by jQuery, it is not possible to upload files using JavaScript through XMLHttpRequest
. The common workaround is to let JavaScript create a hidden <iframe>
and submit the form to it instead so that the impression is created that it happens asynchronously. That's also exactly what the majority of the jQuery file upload plugins are doing such as jQuery Form plugin (example here).
假设您的带有 HTML 表单的 JSP 以这种方式重写,以便在客户端禁用 JS 时它不会损坏(就像您现在...),如下所示:
Assuming that your JSP with the HTML form is rewritten in such way so that it's not broken when the client has JS disabled (as you have now...), like below:
<form id="upload-form" class="upload-box" action="/Upload" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="file1" />
<span id="upload-error" class="error">${uploadError}</span>
<input type="submit" id="upload-button" value="upload" />
</form>
然后借助 jQuery Form 插件就可以了
Then it's with help of jQuery Form plugin just a matter of
<script src="jquery.js"></script>
<script src="jquery.form.js"></script>
<script>
$(function() {
$('#upload-form').ajaxForm({
success: function(msg) {
alert("File has been uploaded successfully");
},
error: function(msg) {
$("#upload-error").text("Couldn't upload file");
}
});
});
</script>
至于servlet端,这里不需要做特别的事情.只需以与不使用 Ajax 时完全相同的方式实现它:如何使用 JSP/Servlet 上传文件到服务器?
As to the servlet side, no special stuff needs to be done here. Just implement it exactly the same way as you would do when not using Ajax: How to upload files to server using JSP/Servlet?
您只需要在 servlet 中额外检查 X-Requested-With
标头是否等于 XMLHttpRequest
,以便您知道什么样的客户端禁用 JS 时返回的响应(截至目前,禁用 JS 的主要是旧版移动浏览器).
You'll only need an additional check in the servlet if the X-Requested-With
header equals to XMLHttpRequest
or not, so that you know how what kind of response to return for the case that the client has JS disabled (as of now, it are mostly the older mobile browsers which have JS disabled).
if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
// Return ajax response (e.g. write JSON or XML).
} else {
// Return regular response (e.g. forward to JSP).
}
请注意,相对较新的 XMLHttpRequest
版本 2 能够使用新的 File
和 FormData
API 发送选定的文件.另请参阅HTML5 文件上传到 Java Servlet 和 通过 xmlHttpRequest 将文件作为多部分发送.
Note that the relatively new XMLHttpRequest
version 2 is capable of sending a selected file using the new File
and FormData
APIs. See also HTML5 File Upload to Java Servlet and sending a file as multipart through xmlHttpRequest.
这篇关于如何使用 JSP/Servlet 和 Ajax 将文件上传到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!