asp.net the idea of renaming a file while downloading it and its implementation method _ practical skills

Source: Internet
Author: User
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>

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.