Microsoft
Software
Hardware
Network
Question : servlet download file with javascript enabled
Hi,
This is what I need to achieve.
I have a client side code that calls a servlet with parameters. The servlet gets the parameters and process it. Generate a zip file and then send it back to client for download. A "Save as" window will appear. It runs great with small download file because the "Save As" window will come out right away. But with larger download file, I would like to have a waiting window so the user will know something is being downloaded. For this purpose, I need to pass some javascript back from the servlet to tell the client that the zip file is done generated so the waiting window can be closed.
This is the piece of code I'm using. I'm able to download the zip file but I don't know how I can include javascript in the response? I've tried multiple server push but according to some research, IE doesn't work for multiple server push. By the way, my front end is using smartgwt. Thanks a lot!
response.setContentType("t
ext/html")
;
ServletOutputStream out = null;
try {
out = response.getOutputStream()
;
// out.write(("<html>").getBy
tes());
// out.write(("<head>").getBy
tes());
//
// out.write(("<script type=\"text/javascript\">"
).getBytes
());
// out.write(("if (parent.uploadComplete) parent.exportComplete('"+ "filename.txt" + "');").getBytes());
// out.write(("</script>").ge
tBytes());
//
// out.write(("</head>").getB
ytes());
} catch (Exception e) {
e.printStackTrace();
}
List<String> exportList = new ArrayList<String>();
String selectedRec = request.getParameter(reque
st.getPara
meter("nam
e"));
String fileId = "";
try {
JSONTokener tok = new JSONTokener(selectedRec);
JSONArray jarray = new JSONArray(tok);
for (int i = 0; i < jarray.length(); i++) {
String path = jarray.getString(i);
logger.debug("jarray " + i + " is " + path);
int type = path.split("/").length;
fileId = path.split("/")[2];
if (type == 3) {
exportList.add("file=" + path);
} else if (type == 5) {
exportList.add("document="
+ path);
} else if (type == 7) {
exportList.add("page=" + path);
}
}
} catch (JSONException e) {
logger.error("Export error", e);
e.printStackTrace();
}
try {
HashMap<String, byte[]> outMap =
ExportUtils.getContentFrom
Repo("expo
rt", exportList);
// Write to the zip file
ByteArrayOutputStream baos = ExportUtils.zipFiles(outMa
p);
response.setHeader("Conten
t-Disposit
ion", "attachment; filename="
+ fileId + ".zip");
try {
//OutputStream out = response.getOutputStream()
;
out.write(baos.toByteArray
());
//out.write(("</html>").ge
tBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
logger.error("Export error", e);
e.printStackTrace();
}
Answer : servlet download file with javascript enabled
You may return first the path/url of the file with some messages (size, filename,...) to the browser then the browser receiving a valid message and url start downloading the file with the url provided
Random Solutions
RSH client for windows 2008 r2
Cannot open help files in WIN7
Clearing a submit form once submitted
Antivirus software windows XP
Error while building Offline address Book, Event 9331
Compare Internet Explorer (IE6) Security Settings
Websphere deployment logs
ASP.NET clickOnce to download app to client
How to change the from field on auto-generated SharePoint emails.
Complete uninstall of Sharepoint Services 3.0 from Windows 2008 Server