What's new

FILE UPLOAD USING APACHE COMMON AND SERVLET 2.5 FILEITEM RETURNS EMPTY

Fever

Active member
Staff member
i have been trying to add an upload button to an existing form that is created using jquery strut 2 elements, so i have added a simple input and a button to submit the files.

this is the code that triggers the file upload

$("#btnUpload").on("click", function() {
console.log("Uploading file");

var url = "UploadExternalFiles";
const myFile = document.getElementById("externalDocument");
/*var form = $("#newExternalForm")[0];
var externalDocsData = new FormData(form);*/
//var fileLenght = myFile.files.length;
var externalDocsData = new FormData()

var files = myFile.files;

var externalDocsData = new FormData()

for(var i = 0; i < files.length; i++){
var file = files;

externalDocsData.append('myfiles[]', file, file.name);
}


//externalDocsData.append("login", vm.user.login);

/*for(var i = 0; i < myFile.files.length; i++) {
externalDocsData.append('externalDocument', myFile.files);
}*/

console.log(Array.from(externalDocsData));

for(let obj of externalDocsData){
console.log(obj)
}

$.ajax({
type : "POST",
encType : "multipart/form-data",
url : url,
cache : false,
processData : false,
contentType : false,
data : externalDocsData,
success : function(msg) {
var response = JSON.parse(msg);
var status = response.status;
if (status == 1) {
alert("File has been uploaded successfully");
} else {
alert("Couldn't upload file");
}
},
error : function(msg) {
alert("Couldn't upload file");
}
});
});


and my servlet side

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try {

List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items)
{
System.out.println("UploadExternalFiles");
}
System.out.println("Done UploadExternalFiles");
} catch (FileUploadException e) {
throw new ServletException("Cannot parse multipart request.", e);
}
doGet(request, response);
}
}


i have checked and the form has the correct enctype,

I have been stuck in this for days now,

Thanks in advanced.

Continue reading...
 
Top