Idea and implementation of Renaming an ASP. NET file during File Download

Source: Internet
Author: User

Sometimes it is not good to check files one by one to ensure that they will not overwrite the previously uploaded files when they are uploaded, therefore, the GUID can be automatically generated here to rename the file name to the original name of GUID. extension. However, it is recommended that the original name be restored during the download process. At this time, it hurts. After searching the relevant information, you can use response to solve the problem. The Code is as follows.

[Csharp]
Copy codeThe Code is as follows:
<Pre name = "code" class = "csharp"> string path = Server. MapPath ("aa \ ahaakladahsasdas_bb.zip ");
String newfileName = "";
If (File. Exists (path ))
{
FileInfo fi = new FileInfo (path );
Response. Clear (); // The Clear method deletes all HTML output in the buffer. However, the Clear method only deletes the response body.
// Do not delete the response title. You can use this method to handle errors.
Response. ClearHeaders ();
Response. Buffer = false; // This statement indicates whether the output page is buffered. When the attribute value is True,
// The server will not send any information to the client until all programs are executed or
// Response. Flush or Response. End statement to release the buffer information.
String filename = Path. GetFileName (path );
NewfileName = newfileName + filename. Substring (filename. LastIndexOf ("."));
Response. Clear ();
Response. ClearHeaders ();
Response. Buffer = false;

NewfileName = HttpUtility. UrlEncode (newfileName); // In this step, 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 =" + newfileName); // The name displayed when you save the file.
// Tell the client that the Response content type is attachment. You declare it through Response. AppendHeader ("content-type", "attachment.

Response. AppendHeader ("Content-Length", fi. Length. ToString ());
Response. ContentType = "application/octet-stream ";
Response. WriteFile (newfileName); // use response to return the file path
Response. Flush (); // clear response
Response. End ();
// Response. End (); if an exception is thrown, use
// HttpContext. Current. ApplicationInstance. CompleteRequest ();
}
Else
{
Response. Write ("<script langauge = javascript> alert ('the file does not exist! '); </Script> ");
}

</Pre> <br>
<Br>
<Pre> </pre>
<P> </p>
<Pre> </pre>
<P> </p>
<Pre> </pre>

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.