如何在Java服务器端使用Ajax检索上传的文件? [英] How to retrieve uploaded file using ajax on java server side?
问题描述
我正在服务器端使用struts2框架.我正在使用
I am using struts2 framework on server side. I am uploading a file using
服务器端:
<s:file name="fTU" id="fTU"/>
<input type="submit" value ="ok" onclick="upload()">
客户端:
function upload(){
var file = document.getElementById("fTU");
try {
this.xml = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
this.xml = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err) {
this.xml = null;
}
}
if(!this.xml && typeof XMLHttpRequest != "undefined")
this.xml = new XMLHttpRequest();
if (!this.xml){
this.failed = true;
}
var formData = new FormData();
/* Add the file */
formData.append("upload", file.files[0]);
xml.open("POST",",true); xml.setRequestHeader("Content-Type","false");
xml.open("POST", "", true); xml.setRequestHeader("Content-Type", "false");
xml.send(formData); /* Send to server */
xml.onreadystatechange = function () {
if (xml.readyState == 4 && xml.status == 200) {
alert(xml.statusText);
}
}
}
如何在struts2服务器端获取上载的文件对象?
How to fetch the uploaded file object on struts2 server side?
它正在服务器端类中,我正在尝试使用request.getParameter(upload)来检索文件,但它给出的是空值.
It is going in server side class and I am trying to retrieve file by using request.getParameter(upload) but it is giving null.
推荐答案
我认为您错过了在xml.open()
方法中添加操作链接的过程.看看 MDN 可以看到一些示例.
I think you missed to add the Action Link in the xml.open()
method. Take a look at the MDN to see some examples.
您的Action
类的外观如何?您是否已使用getter/setter定义了一个名为 upload 的File
字段?
How does your Action
-class look like? Have you defined a File
field named upload with getter/setters?
还请检查您的浏览器是否支持FormData
元素,请参见问题.
Please also check, that your browser supports the FormData
element, see question.
我还建议您使用jQuery之类的库来简化Javascript代码.
I also would suggest you to use a library like jQuery to simplify the Javascript code.
操作
public class FileUpload extends ActionSupport {
File myFile;
public String execute() {
//do some preparations for the upload
return SUCCESS;
}
public String upload() {
//only here the myFile is filled with data
return SUCCESS;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public File getMyFile() {
return myFile;
}
}
struts.xml
<!-- init method to show the form -->
<action name="fileForm" class="...FileUpload">
<result name="success">fileupload.jsp</result>
</action>
<!-- upload method to upload the file -->
<action name="fileUpload" class="...FileUpload" method="upload">
<result name="success">fileupload.jsp</result>
</action>
JSP
<s:form enctype="multipart/form-data" method="post" name="fileinfo" action="fileUpload">
<s:file name="myFile"/>
</s:form>
<input type="submit" onClick="upload()" value="OK">
JavaScript(摘自上面的MDN示例)
function upload() {
var fd = new FormData(document.querySelector("form"));
$.ajax({
url : "fileUpload", //this is the actionName
type: "POST",
data: fd,
processData: false,
contentType: false
});
return false; //to stop submitting the form
}
我尚未测试此代码,因此请对其进行更改或在某些操作无效的情况下添加注释.
I have not tested this code, so please change it or add comments if something doesn't work.
这篇关于如何在Java服务器端使用Ajax检索上传的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!