Asp.net mode window File Download Solution

Source: Internet
Author: User
The idea of http://blog.163.com/red_guitar@126/blog/static/117206128201123845276/, put a shadow iframe in the modal window page.
First, we create a new modal window in the modal window a. aspx Java code
  1. <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <Html xmlns = "http://www.w3.org/1999/xhtml">
  3. <Head runat = "server">
  4. <Base target = "_ self"/>
  5. <Title> </title>
  6. </Head>
  7. <Body>
  8. <Form id = "form1" runat = "server">
  9. <Center>
  10. <Asp: LinkButton ID = "ibDownLoad" runat = "server" onclick = "ibDownLoad_Click"> click here to download the file </asp: LinkButton>
  11. </Center>
  12. <Iframe id = "download" runat = "server" style = "display: none;"> </iframe>
  13. </Form>
  14. </Body>
  15. </Html>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

Background code: Java code

  1. Protected VoidIbDownLoad_Click (object sender, EventArgs e)
  2. {
  3. String filePath = Server. MapPath ("\ PDF \ a.txt ");
  4. Download. Attributes ["src"] = "DownLoad. aspx? FilePath = "+ filePath;
  5. }
protected void ibDownLoad_Click(object sender, EventArgs e)          {              string filePath = Server.MapPath("\\PDF\\a.txt");              download.Attributes["src"] = "DownLoad.aspx?filePath=" + filePath;           }

Process DownLoad. aspx on the file DownLoad page. The page code is not written and written in the background code: Java code

  1. Protected VoidPage_Load (object sender, EventArgs e)
  2. {
  3. String filePath = Request. QueryString ["filePath"];
  4. // String filePath = Server. MapPath ("\ PDF \ a.txt ");
  5. If(File. Exists (filePath ))
  6. {
  7. FileInfo file =NewFileInfo (filePath );
  8. Response. ContentEncoding = System. Text. Encoding. GetEncoding ("UTF-8"); // solve Chinese garbled characters
  9. Response. AddHeader ("Content-Disposition", "attachment; filename =" + Server. UrlEncode (file. Name); // fix Chinese file Name garbled characters
  10. // Response. AddHeader ("Content-Disposition", "attachment; filename =" + file. Name );
  11. Response. AddHeader ("Content-length", file. Length. ToString ());
  12. Response. ContentType = "appliction/octet-stream ";
  13. Response. WriteFile (file. FullName );
  14. Response. End ();
  15. }
  16. }
Protected void Page_Load (object sender, EventArgs e) {string filePath = Request. queryString ["filePath"]; // string filePath = Server. mapPath ("\ PDF \ a.txt"); if (File. exists (filePath) {FileInfo file = new FileInfo (filePath); Response. contentEncoding = System. text. encoding. getEncoding ("UTF-8"); // solves Chinese garbled Response. addHeader ("Content-Disposition", "attachment; filename =" + Server. urlEncode (file. name); // solves Chinese file Name garbled characters // Response. addHeader ("Content-Disposition", "attachment; filename =" + file. name); Response. addHeader ("Content-length", file. length. toString (); Response. contentType = "appliction/octet-stream"; Response. writeFile (file. fullName); Response. end ();}}

OK. This will solve some problems such as clicking the download link in the modal window, or the pop-up prompt. From: http://269181927.javaeye.com/blog/865034

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.