I want to create an Excel File, if the user pressed a formular button on my webpage. My first solution works fine. The User pressed the button, the serverside java code creates the file and after the java code was executed the webbrowser open a dialog, which aks the user whether to show or to save the excel file.
But I get some new requirements from my customer. If the user pressed the formular button, it is necessary to show a animated gif image while the server side java code was executed and the animated gif must be disapeared immediately, if the server side java code was finished.
This is the Struts2 submit Button, which starts the HTTP Request, if the button was pressed:
<s:submit value="show Data" onclick="myJsFunction(); return false;" />
This is the client code, which creates and sends the HTTP Request to my Struts2 Actionclass:
function myJsFunction(){
$.ajax({
type: "POST",
dataType: 'binary',
url: "myActionClass.action",
data: {//some necessary input values},
success: function(data){
console.log(data);
// js code to disappeared the animated gif image
}
});
}
This is the Java code, which create the Binarycode:
private void returnExcelFileAsStream(final String filename) throws IOException {
final InputStream is = new FileInputStream(filename);
OutputStream os = null;
try {
response.setContentType("application/pdf");
response.setHeader("Cache-Control", "maxage=3600");
response.setHeader("Pragma", "public");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
os = response.getOutputStream();
int len;
byte buf[] = new byte[1024];
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
is.close();
os.close();
}
catch(Exception e){
// some exeception handling
}
}
This is the Struts2 execute method, which called the method returnExcelFileAsStream.
public String execute(){
// some Java code
returnExcelFileAsStream("MyExcelFile.xlsx")
return null;
}
This is my struts.xml file:
<action name="myActionClass" class="myPackage.myActionClass">
<result name="input" type="redirectAction">/WEB-INF/base/jsp/myJspPage.jsp</result>
</action>
Now a have the problem, that the webbrowser doesn´t open a user dialog, which asked the user to show or to save the file. With firebug I see some cryptical characters in the HTTP Response. How can I solve this problem?