In this article I will explain with an example, how to rename File before download in ASP.Net using C# and VB.Net.
The new name of the File will be set in the Header of the Response Stream and then the File will be sent for download at the Client end in ASP.Net using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net Button control.
<asp:Button Text="Download File" runat="server" OnClick="DownloadFile" />
 
 
Renaming (Changing) File before Download in ASP.Net
The File is stored in a Folder (Directory) named Files within the Project Folder.
When the Download File Button is clicked, first the Content Type and the Header for the File are set.
The new name of the File is set in the Header of the Response Stream.
Finally, the File is written to the Response Stream and the Response is terminated.
C#
protected void DownloadFile(object sender, EventArgs e)
{
    //File to be downloaded.
    string fileName = "CustomerReport.pdf";
 
    //Set the New File name.
    string newFileName = "Report.pdf";
 
 
    //Path of the File to be downloaded.
    string filePath = Server.MapPath(string.Format("~/Files/{0}", fileName));
 
    //Setting the Content Type, Header and the new File name.
    Response.ContentType = "application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + newFileName);
 
    //Writing the File to Response Stream.
    Response.WriteFile(filePath);
    Response.Flush();
   Response.End();
}
 
VB.Net
Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs)
    'File to be downloaded.
    Dim fileName As String = "CustomerReport.pdf"
 
    'Set the New File name.
    Dim newFileName As String = "Report.pdf"
 
    'Path of the File to be downloaded.
    Dim filePath As String = Server.MapPath(String.Format("~/Files/{0}", fileName))
 
    'Setting the Content Type, Header and the new File name.
    Response.ContentType = "application/pdf"
    Response.AppendHeader("Content-Disposition", ("attachment; filename=" + newFileName))
 
    'Writing the File to Response Stream.
    Response.WriteFile(filePath)
    Response.Flush()
    Response.End()
End Sub
 
 
Screenshot
Rename (Change) File before Download in ASP.Net using C# and VB.Net
 
 
Downloads