Rename an ASP. NET file during download

Source: Internet
Author: User

String fileulr = server. mappath ("~ /Images/test/"); // obtain the directory of the object

Fileulr + = "5.jpg"; // obtain the location where an existing file is located. String outfilename =" newfilename "; if (file. exists (fileulr) // determines whether the file exists {fileinfo Fi = new fileinfo (fileulr); response. clear (); response. clearheaders (); response. buffer = false; string tempfile = path. getfilename (fileulr); // obtain the file name tempfile = outfilename + tempfile. substring (tempfile. lastindexof (". "); response. clear (); response. clearheaders (); response. buffer = Fals E; // The Chinese file name is also normal during this step of debugging. String downloadfilename = NULL; string browser = This. context. request. useragent. toupper (); If (browser. contains ("Ms") = true & browser. contains ("ie") = true) // determines the browser type used by the current user {downloadfilename = httputility. urlencode (tempfile); // The Chinese file name is also normal during this step of debugging. In IE, Chinese characters are displayed normally, but in Firefox, Chinese characters are still // garbled characters. Therefore, we need to judge whether the user uses the browser type, to keep the normal display of Chinese file names} else if (browser. contains ("Firefox") = true) {downloadfilename = "\" "+ tempfile +" \ "";} else {downloadfilename = httputility. urlencode (tempfile); // The download and save dialog box is displayed. The file name is garbled, but the file name in the variable is normal.} Response. appendheader ("content-disposition", "attachment; filename =" + downloadfilename); // save the file as the display name response. appendheader ("Content-Length", Fi. length. tostring (); response. contenttype = "application/octet-stream"; response. writefile (fileulr); // use response to return the file path response. flush (); // clear response. end ();} else {response. write ("<SCRIPT langauge = JavaScript> alert ('the file does not exist! '); </SCRIPT> "); response. end ();} In fact, the core of this method is in response. appendheader ("content-dispostion", "attachment, filename =" + downloadfilename );
Content-dispostion is an extension of the mime protocol, which instructs the mime user agent how to display additional files. When Internet Explorer receives the header, it will activate the File Download Dialog Box, and its file clearly box will automatically fill in the file name specified in the header. This is caused by design. You cannot use this function to store documents on your computer, instead of asking users about the storage location. Of course, this situation does not exist in ie9, and ie9 will be automatically saved in the folder set at the beginning. Ie9 is saved in the user/download folder by default.
Here, the entire download and rename will be completed. Different comments are welcome. From: http://www.liushuige.net/newweb/html/news/info.aspx? Id = 32
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.