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
How can I convert a custom control that calls User32 and Shell32 to work on Mono?
Java web service accesing error
Cron Error with 1 and 1 dedicated server - linux
To change asni_padding setting of a column.
Sharepoint Webpart page load event issue
Windows 7 Sounds Controls (device at Volume 0 but can still hear)
Intel Sata RAID controller on RHEL 4 problem
OCS Edge Server R2 2007
Sending pdf files to client using webmethods
Cisco Unity Express to Trigger MWI on overlay DN