Click here to Skip to main content
16,016,736 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How to download a file from server in .net?
Thanks.
DK Dixit
Posted

use following code

C#
System.String filename = "myFile.txt";

// set the http content type to "APPLICATION/OCTET-STREAM
Response.ContentType = "APPLICATION/OCTET-STREAM";
   
// initialize the http content-disposition header to
Response.AppendHeader("Content-Disposition", filename);

 // transfer the file byte-by-byte to the response object
System.IO.FileInfo fileToDownload = new System.IO.FileInfo("C:\\myFile.txt");
Response.Flush();
Response.WriteFile(fileToDownload.FullName);}
 
Share this answer
 
See CP article
File Download
 
Share this answer
 
 
Share this answer
 
As Mr.koolprasd2003 mentioned,
you have to set
Response.ContentType first, but to know the content type by using fileextenctions you can use this method
C#
public string getContentType(string extension)
       {
           const string DEFAULT_CONTENT_TYPE = "application/unknown";

           RegistryKey regkey, fileextkey;
           string filecontenttype;


           string fileextension = extension;

           try
           {

               regkey = Registry.ClassesRoot;


               fileextkey = regkey.OpenSubKey(fileextension);


               filecontenttype = fileextkey.GetValue("Content Type",         DEFAULT_CONTENT_TYPE).ToString();


               fileextkey = null;
               regkey = null;

           }
           catch
           {
               filecontenttype = DEFAULT_CONTENT_TYPE;
           }



           return filecontenttype;


       }

C#
Response.ContentType = getContentType(".jpg");
Response.AppendHeader("Content-Disposition", "Filename");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900