Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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.

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

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Posted

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..

 

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();

Posted

Have you imported System.IO and System.NET?

 

In relation to converting VB.NET to C# or C# to VB.NET, I use the following site: http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx

Just paste your code into the textfield provided, click the convert button, and hey presto you have the code in the required language.

 

Mike55

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Posted (edited)

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?

Edited by a1jit
Posted (edited)

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

 

			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.

 

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?

Edited by a1jit
  • Administrators
Posted
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();

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...