JSP File Download demo
1. Front-end
2. Background
1. Front-end
Http://www.javaeye.com/topic/446631
Why download files in JS mode? Because dynamic computing is required based on your clicks and selections.
If you directly request Ajax, there will always be some problems... the specific manifestation is that the file is directly returned as a string.
First, solution 1: Use window. Open (URL). This will cause a problem because a window will be opened! This window will be closed when you click Download, but it looks really bad!
Solution 2: Use IFRAME. For details, seeCode:
JavaScript code
-
- FunctionDownloadfile (URL ){
- VaRElemif = Document. createelement ("Iframe");
-
- Elemif. src = URL;
- Elemif. style. Display ="None";
-
- Document. Body. appendchild (elemif );
- }
Perfect solution!
(PS: discovering the construction language to describe and understand one thing is really a tiring job. You can't understand it, Pat it on your head)
-
- Ext. Ajax. Request ({
- URL:'Getpath. action',
-
- Success:Function(RES ){
- VaROBJ = ext. Decode (res. responsetext );
-
- // Console. Log (OBJ); // you can view the structure in OBJ under firebug of Firefox.
- // Add the JSON returned by getpath to {'path': 'upload/abc.jpg '}
-
- Window. Location. href = obj. path;// The download dialog box is displayed.
- }
-
- });
The Save dialog box is displayed as long as the linked file exists.
<A href = "test. rmvb"> download this file </a>
If you must use JS, there are two methods. The first one is recommended:
Method 1: // The advantage is that if the downloaded file does not exist, the browser address will not change. Otherwise, the system prompts to save the file.
HTML code <a href = "#" onclick = "download () "> download an object </a> <IFRAME id =" DownLoadURL "Height =" 0 "width =" 0 "src =" "> </iframe> <script language =" Javascript "> function download () {document. getelementbyid ("DownLoadURL "). src = "test. rmvb ";}</SCRIPT>
Method 2: // The disadvantage is that the browser address changes if the downloaded file does not exist.
HTML code <a href = "#" onclick = "download (this)"> download an object </a> <script language = "JavaScript"> function download (OBJ) {obj. href = "test. rmvb ";}</SCRIPT>
2. Background Program
Public void downloadpdf (httpservletrequest request, httpservletresponse response) <br/> throws exception {<br/> string id = request. getparameter ("projectid"); </P> <p> string filename = This. dao. querydocfillname (ID); <br/> string Path = request. getcontextpath () + "/frontweb/onecasefile/approvefile/" + filename + ". PDF "; </P> <p> file pdffile = new file (request. getrealpath ("/"). concat (<br/> "frontweb/onecas Efile/approvefile /"). replace ("\", "/") + filename + ". PDF "); <br/> response. setcontenttype ("application/pdf"); <br/> response. setcontenttype ("application/force-download"); <br/> response. addheader (<br/> "content-disposition", "attachment; filename =" + "Download"); <br/> response. setcontentlength (INT) pdffile. length (); </P> <p> fileinputstream input = new fileinputstream (pdffile); <br/> bufferedinpu Tstream bufferedinputstream = new bufferedinputstream (input); <br/> int readbytes = 0; <br/> while (readbytes = bufferedinputstream. Read ())! =-1) <br/> response. getoutputstream (). Write (readbytes); <br/>}