a1jit Posted September 12, 2005 Posted September 12, 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? Quote
Administrators PlausiblyDamp Posted September 12, 2005 Administrators Posted September 12, 2005 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
a1jit Posted September 12, 2005 Author Posted September 12, 2005 Hi, could you explain again..I dont get you.. Which part of my code shall i change? Quote
mike55 Posted September 12, 2005 Posted September 12, 2005 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 Quote 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)
a1jit Posted September 13, 2005 Author Posted September 13, 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.. 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(); Quote
mike55 Posted September 13, 2005 Posted September 13, 2005 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 Quote 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)
a1jit Posted September 13, 2005 Author Posted September 13, 2005 (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 September 13, 2005 by a1jit Quote
mark007 Posted September 13, 2005 Posted September 13, 2005 That's what PlausiblyDamp was saying - write the file directly to the output stream accessed via Response.OutputStream. :) Quote Please check the Knowledge Base before you post. "Computers are useless. They can only give you answers." - Pablo Picasso The Code Net
a1jit Posted September 14, 2005 Author Posted September 14, 2005 (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 September 14, 2005 by a1jit Quote
Administrators PlausiblyDamp Posted September 14, 2005 Administrators Posted September 14, 2005 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(); Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
a1jit Posted September 14, 2005 Author Posted September 14, 2005 I see..Thank You very much Plausibly..really appreciate it..thxss.. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.