Saturday, 30 August 2014

How to download a text file from server?

To download a text file from server use the following code:

           
                string file_path="~/files/file1.txt";    //relative path of your file on server
                string filename="myfile.txt";            //your file will be downloaded with this name
                System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
                response.ClearContent();
                response.Clear();
                response.ContentType = "text/plain";
                response.AddHeader("Content-Disposition", "attachment; filename=" +filename+ ";");                                   response.TransmitFile(Server.MapPath(file_path ));
                response.Flush();
                response.End();  

             

No comments:

Post a Comment