java - How to create and Download Excel File by using Ajax request -


i want create excel file, if user pressed formular button on webpage. first solution works fine. user pressed button, serverside java code creates file , after java code executed webbrowser open dialog, aks user whether show or save excel file.

but new requirements customer. if user pressed formular button, necessary show animated gif image while server side java code executed , animated gif must disapeared immediately, if server side java code finished.

this struts2 submit button, starts http request, if button pressed:

<s:submit value="show data" onclick="myjsfunction(); return false;" /> 

this client code, creates , sends http request 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 disappeared animated gif image                                                  }       });                 } 

this java code, create binarycode:

private void returnexcelfileasstream(final string filename) throws ioexception {     final inputstream = 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){          // exeception handling      } } 

this struts2 execute method, called method returnexcelfileasstream.

public string execute(){      // java code     returnexcelfileasstream("myexcelfile.xlsx")      return null; } 

this struts.xml file:

<action name="myactionclass" class="mypackage.myactionclass">         <result name="input" type="redirectaction">/web-inf/base/jsp/myjsppage.jsp</result> </action> 

now have problem, webbrowser doesn´t open user dialog, asked user show or save file. firebug see cryptical characters in http response. how can solve problem?

i don't see action in javascript success function trigger browser navigate through newly created excel file. sorry if told in text missed it, in way:

  • my server side creates new xls or csv file location (folder, /files) temporary excel or csv files exist in it. server side function returns name of newly created file
  • on success method of ajax, execute following:

    document.location='files/[the newly generated file's name].xls';

then browser redirects file, either displays or ask user download

finally, server side job clears folder 1 day old or older files

i hope helps


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -