Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to rename a file when it is downloaded by ASP.NET

2025-01-16 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report