In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how ASP.NET can rename a file when downloading. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Sometimes in order to ensure that the files uploaded again will not overwrite the previously uploaded files, and because there may be many files in the uploaded target directory, it is not good to check one by one at this time, so you can automatically generate GUID to rename the file name to the original GUID_ name. Extension name. However, it is best to ensure that the original name is restored when downloading. It hurts to listen to it at this time. After searching the relevant information, we know that response can be used to solve the problem. The specific code is as follows.
String path = Server.MapPath ("aa\\ ahaakladahsasdas_bb.zip")
String newfileName = ""
If (File.Exists (path))
{
FileInfo fi = new FileInfo (path)
The Response.Clear (); / / Clear method deletes all HTML output from the buffer. However, the Clear method only deletes the response body
/ without deleting the response title. You can use this method to handle error situations.
Response.ClearHeaders ()
The sentence Response.Buffer = false;// means to indicate 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 have been executed or encountered
/ / Response.Flush or Response.End statement before the buffer information is released.
String filename = Path.GetFileName (path)
NewfileName = newfileName + filename.Substring (filename.LastIndexOf (".")
Response.Clear ()
Response.ClearHeaders ()
Response.Buffer = false
NewfileName = HttpUtility.UrlEncode (newfileName); / / this step pops up the download and save dialog box, and the file name is garbled, but the file name in the variable is normal.
Response.AppendHeader ("Content-Disposition", "attachment;filename=" + newfileName); / / Save the file for the user is the name displayed
/ / tell the client that the type of response content is attachment. You are declared 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 path of the file
Response.Flush (); / / clear response
Response.End ()
/ / Response.End (); can be used if an exception is thrown
/ / HttpContext.Current.ApplicationInstance.CompleteRequest ()
}
Else
{
Response.Write ("alert ('file does not exist!')
}
This is the end of the article on "how to rename a file when downloading by ASP.NET". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.