Link to home
Start Free TrialLog in
Avatar of Charles Baldo
Charles BaldoFlag for United States of America

asked on

File Downlaod

I am creating a .csv file on my server then I want to be able to let the user download it.  I have this code and it creates a file but instead of letting them download it displays it on the browser

            // create a writer and open the file
            TextWriter tw = new StreamWriter(@"C:\Inetpub\websites\website.net\app_data\downloads\work.csv");

            // write a line of text to the file
            tw.WriteLine("test,123,99.85,$100.34");

            // close the stream
            tw.Close();


            Response.Write("Download");
            Response.ContentType = "text/csv";
            Response.AddHeader("content-disposition", "attachment; filename=work.csv");

            FileStream sourceFile = new FileStream(@"C:\Inetpub\websites\website.net\app_data\downloads\work.csv", FileMode.Open);
            long FileSize;
            FileSize = sourceFile.Length;
            byte[] getContent = new byte[(int)FileSize];
            sourceFile.Read(getContent, 0, (int)sourceFile.Length);
            sourceFile.Close();

               Response.BinaryWrite(getContent);
SOLUTION
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial