Create csv file and download it

a1jit

Regular
Joined
Aug 19, 2005
Hi Guys,

I was just wondering, how can i achieve this..
I want to create a csv file on server side and prompt user to download it using c# with asp.net

I have written some codes which create a dummy text file..and some download code..but the download code does not fetch the actual test.csv file which i created, instead it gives me the html code where the download dialogue box was prompted

StreamWriter streamWriter = new StreamWriter("C:\\Mail\\test.txt");
streamWriter.WriteLine("Date : ");
streamWriter.WriteLine("From : " );
streamWriter.WriteLine("To : ");
streamWriter.WriteLine("Subject : ");
streamWriter.Close( );

Response.ContentType = "text/plain";
Response.AddHeader( "Content-Disposition", "attachment; Filename = C:\\Mail\\test.txt");

How can i solve this?
 

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Location
Lancashire, UK
Rather than writing to a file and then attempting to send the file to the client you could just write direct to Response.OutputStream, just continue to set the content type correctly and it should work fine.
 

mike55

Contributor
Joined
Mar 26, 2004
Location
Ireland
To download a file, you simple call the following method passing in the file path and file name of the file you want to download.
Code:
 Private Sub downloadFile(ByVal filePath As String, ByVal FileName As String)
        Dim fs As FileStream
        Dim strContentType As String

        'Read the file into a file stream
        fs = File.Open(filePath, FileMode.Open) 'Open the file.
        Dim byBytes(Convert.ToInt32(fs.Length)) As Byte
        fs.Read(byBytes, 0, Convert.ToInt32(fs.Length))
        fs.Close()

        'Delete file.
        File.Delete(filePath)

        'Offer use the chance to either save, open or cancel.
        Response.AddHeader("Content-disposition", "attachment; fileName=" + FileName)
        Response.ContentType = "application/octet-stream"
        Response.BinaryWrite(byBytes)
        Response.End()
    End Sub

After using this code to do the download for a .CSV file - so it does work.
Mike55
 

a1jit

Regular
Joined
Aug 19, 2005
Hi ..thankx a lot for the help..

I would appreaciate if you can help me converting it to c#

I have done this..but results in some error..

Code:
FileStream fs = new FileStream();
		    string strContentType;

			StreamWriter streamWriter = new StreamWriter("C:\\Mail\\test.txt");
			streamWriter.WriteLine("Date	: ");
			streamWriter.WriteLine("From	: " );
			streamWriter.WriteLine("To		: ");
			streamWriter.WriteLine("Subject	: ");
			streamWriter.Close( );

			fs = File.Open("C:\\Mail\\test.txt", FileMode.Open);
			Byte byBytes(Convert.ToInt32(fs.Length));
			fs.Read(byBytes, 0, Convert.ToInt32(fs.Length));
			fs.Close();

			
			File.Delete("C:\\Mail\\test.txt");

			
			Response.AddHeader( "Content-Disposition", "attachment; Filename = test.txt");
			Response.ContentType = "application/octet-stream";
			Response.BinaryWrite(byBytes);
			Response.End();
 

a1jit

Regular
Joined
Aug 19, 2005
Mike,

U are just great..its working perfectly now..and the link you provided is excellent..
reallyyyy good..
thank you very very much

But i have a question..

Currently im creating the file first on server, then after that, i give the user to download it..

How can i straight away let user to download it while the file is being created?
 
Last edited:

a1jit

Regular
Joined
Aug 19, 2005
So, where shall i place the Responce.OutputStream statement?
Thanks..really sorry..im a bit new to this

And another question, im using the code you provided to download the file

Code:
			FileStream fs;
			fs = File.Open("C:\\Inetpub\\wwwroot\\Web Directory\\test.txt", FileMode.Open); 
			byte[] byBytes = new byte[Convert.ToInt32(fs.Length)]; 
			fs.Read(byBytes, 0, Convert.ToInt32(fs.Length)); 
			fs.Close(); 

			File.Delete("C:\\Inetpub\\wwwroot\\Web Directory\\test.txt"); 
		
			Response.AddHeader("Content-disposition", "attachment; fileName=" + "test.txt"); 
			Response.ContentType = "application/octet-stream"; 
			Response.BinaryWrite(byBytes); 
			Response.End();

If you observer, the file is a txt file..works fine

But if i change the extension to a .zip file, i get the following error..
How can i solve this. I have given full access to everyone in the security tab
in the folder, but i still keep getting this error

Exception Details: System.UnauthorizedAccessException: Access to the path "C:\Inetpub\wwwroot\Web Directory\test.zip" is denied.

To grant ASP.NET write access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Code:
Line 32: 			FileStream fs;
Line 33: 			fs = File.Open("C:\\Inetpub\\wwwroot\\Web Directory\\test.zip", FileMode.Open); 
Line 34: 			byte[] byBytes = new byte[Convert.ToInt32(fs.Length)]; 
Line 35: 			fs.Read(byBytes, 0, Convert.ToInt32(fs.Length));

Line 33 is throwing out the error..How shall i deal with this?
 
Last edited:

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Location
Lancashire, UK
C#:
StreamWriter streamWriter = new StreamWriter(Response.OutputStream);
streamWriter.WriteLine("Date	: ");
streamWriter.WriteLine("From	: " );
streamWriter.WriteLine("To		: ");
streamWriter.WriteLine("Subject	: ");
streamWriter.Close( );
			
Response.AddHeader( "Content-Disposition", "attachment; Filename = test.txt");
Response.ContentType = "application/octet-stream";
Response.End();
 
Top Bottom