File Uploads 2 with Ajax

I upload a file using struts 2 with jsp as front end, but I donโ€™t want to refresh the page after downloading the file, so I use Ajax, but with this I canโ€™t get the File object in action, it seems like a form tag is required to load the file in jsp, and if I submit the form, the page will be refreshed. I researched through the network, but I canโ€™t get many relevant results, it would be very useful if someone guides me through this, is there a way for this. Any help would be really appreciated. Regards

+4
source share
1 answer

I suggest using iframe to load the file instead of ajax,

Sample code for loading a Csv file using struts2 and iframe:

var file = $("#fileUpload").val(); if(file.indexOf(".") != -1 && file.substr(file.indexOf("."))==".csv"){ /* created IFrame For UPload file*/ var iframe = $('<iframe name="uploadIPAddressIFrame" id="uploadIPAddressIFrame" style="display: none" />'); $("body").append(iframe); /* Set Form for submit iframe*/ var form = $('#ipPoolForm'); form.attr("action", "uploadCSVFile.do"); form.attr("target", "uploadIPAddressIFrame"); form.submit(); openDialog(title); /* handle response of iframe */ $("#uploadIPAddressIFrame").load(function () { response = $("#uploadIPAddressIFrame")[0].contentWindow.document.body.innerHTML; $("#chkIPAddressDiv").html(response); $("iframe#uploadIPAddressIFrame").remove(); }); 

After loading, if you submit the form, change the purpose of the form:

 // Because of using iframe for upload set target value $("#ipPoolForm").attr("target", ""); 
0
source

Source: https://habr.com/ru/post/1399394/


All Articles