Thursday, April 23, 2015

ASP.Net C# 3 simple ways to prompt a file download using Response.BinaryWrite, Response.Write, Response.WriteFile

Here are 3 simple DowloadFile methods to use in your ASP.Net C# base page or utilities class to simplify and standardize providing a file download prompt in the browser.

Option 1 - Pass in the raw byte array
     public void DownloadFile(byte[] data, string FileType = "application/csv", string FileName = "data.csv")  
     {  
       // headers  
       Response.BufferOutput = true;  
       Response.ClearHeaders();  
   
       // explicitly append preamble for csv files so excel recognizes utf-8 encoding  
       if (FileType.ContainsValue("csv"))  
       {  
         Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());  
       }  
   
       // encoding  
       Response.ContentEncoding = System.Text.Encoding.UTF8;  
   
       // content type  
       Response.ContentType = FileType;  
   
       // content disposition  
       Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", FileName));  
   
       // data  
       Response.BinaryWrite(data);  
   
       // end  
       Response.End();  
     }  

Option 2 - Pass in a simple string, for example HTML data or CSV data
     public void DownloadFile(string data, string FileType = "application/csv", string FileName = "data.csv")  
     {  
       // headers  
       Response.BufferOutput = true;  
       Response.ClearHeaders();  
   
       // explicitly append preamble for csv files so excel recognizes utf-8 encoding  
       if (FileType.ContainsValue("csv"))  
       {  
         Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());  
       }  
   
       // encoding  
       Response.ContentEncoding = System.Text.Encoding.UTF8;  
   
       // content type  
       Response.ContentType = FileType;  
   
       // content disposition  
       Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", FileName));  
   
       // data  
       Response.Write(data);  
   
       // end  
       Response.End();  
     }  

Option 3 - Pass in the file path to a file that already physically exists
     public void DownloadFile(string FileName, string FileType = "application/csv")  
     {  
       // make sure file exists  
       if (!File.Exists(FileName))  
       {  
         return;  
       }  
   
       // headers  
       Response.BufferOutput = true;  
       Response.ClearHeaders();  
   
       // encoding  
       Response.ContentEncoding = System.Text.Encoding.UTF8;  
   
       // content type  
       Response.ContentType = FileType;  
   
       // content disposition  
       Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", Path.GetFileName(FileName)));  
   
       // data  
       Response.WriteFile(FileName);  
   
       // end  
       Response.End();  
     }  

Example usage:
     // download file  
     base.DownloadFile(data: RawBytes, FileType: "application/pdf", FileName: "test.pdf");  

0 comments: