Sometimes in order to ensure that the file upload will not overwrite the uploaded files, and because the upload of the target directory may be a lot of files, this time a check is not very good thing, so here can automatically generate a GUID to rename the filename to guid_ the original name. Extension name. However, it is best to be able to restore the original name when downloading. This time to listen to the troublesome. After searching the relevant information, we know that response can be used to solve the problem. The specific code is as follows.
[CSharp]
Copy Code code 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. But the clear method deletes only the response body
Instead of deleting the response headers. You can use this method to handle error conditions.
Response.ClearHeaders ();
Response.Buffer = false;//The meaning of this sentence is to indicate whether the output page is buffered, and when the property value is true,
The server will not send any information to the client until all programs have finished executing or encountered
Response.Flush or Response.End statements to release information about the buffer.
string filename = Path.getfilename (Path);
NewFileName = newfilename + filename. Substring (filename. LastIndexOf ("."));
Response.Clear ();
Response.ClearHeaders ();
Response.Buffer = false;
NewFileName = Httputility.urlencode (NewFileName);//This step pop-up download saved dialog box, the file name garbled, but the file name in the variable is normal.
Response.appendheader ("Content-disposition", "attachment;filename=" + newfilename);//save file for user is the name displayed
Tell the client the type of response content is attachment you are through Response.appendheader ("Content-type", "attachment"); Affirmed.
Response.appendheader ("Content-length", fi. Length.tostring ());
Response.ContentType = "Application/octet-stream";
Response.WriteFile (NewFileName)//Use Response to return the path to the file
Response.Flush ()//Empty Response
Response.End ();
Response.End (); If you throw an exception, you can use the
HttpContext.Current.ApplicationInstance.CompleteRequest ();
}
Else
{
Response.Write ("<script langauge=javascript>alert" (' file does not exist! '); </script> ");
}
</pre><br>
<br>
<pre></pre>
<p></p>
<pre></pre>
<p></p>
<pre></pre>