我已按照以下方式提出了ajax请求
**function uploadFiles(url, files) {
alert("sss");
var formData = new FormData();
for (var i = 0, file; file = files[i]; ++i) {
alert("v"+file.name);
formData.append("file", file);
}
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.onload = function(e) {alert("'ddd111000") };
xhr.send(formData); // multipart/form-data
}**
它将文件发送到服务器,但是当我尝试读取请求和文件时,它在strut 1.1中使用apache文件上传。它返回我的FileItem对象列表。请帮助我以下是我写的服务器端代码
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(5000 * 1024);
// Location to save data that is larger than maxMemSize.
factory.setRepository(new File("G:\\Project\\"));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( 5000 * 1024 );
// Set factory constraints
// Create a new file upload handler
//ServletFileUpload upload = new ServletFileUpload(factory);
// ServletFileUpload upload = new ServletFileUpload();
// upload.getItemIterator(request);
List items = upload.parseRequest(request);
项目列表为空。请给我代码或想法。
答案 0 :(得分:0)
您无法使用普通AJAX上传文件。 许多JS库都有解决方法,所以最好使用一些像Jquery这样的JS库。