JSP File Download demo

Source: Internet
Author: User
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  
  1. FunctionDownloadfile (URL ){
  2. VaRElemif = Document. createelement ("Iframe");
  3. Elemif. src = URL;
  4. Elemif. style. Display ="None";
  5. Document. Body. appendchild (elemif );
  6. }

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)

  1. Ext. Ajax. Request ({
  2. URL:'Getpath. action',
  3. Success:Function(RES ){
  4. VaROBJ = ext. Decode (res. responsetext );
  5. // Console. Log (OBJ); // you can view the structure in OBJ under firebug of Firefox.
  6. // Add the JSON returned by getpath to {'path': 'upload/abc.jpg '}
  7. Window. Location. href = obj. path;// The download dialog box is displayed.
  8. }
  9. });

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/>}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.