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>