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
Configuration Status error on TMG 2010 Enterprise
Setting Up Coldfusion Builder to Use A Remote Server
java.lang.NoClassDefFoundE
rror: TestZone/java
Localized Date format
how do i stop my queries from being stored
Inline Problem
Need VB code to Autonumber an Alphanumeric field...
VBS Script to monitor MX record changes
Identify keyboard layout
about ORA-00904: "ZIP": invalid identifier