Create and Download A Text File From A Web Page
Create and Download A Text File From A Web Page
home articles quick answers discussions features community help Search for articles, questions, tips
This article will demonstrate how to create a text file and view it.
Introduction
It is often a common requirement in a web application to have the ability to download some sort of file to the client's computer. This
article will illustrate how to create and download a text file to the user's computer.
We first open the file for reading and we actually read the file byte for byte into a stream, then once we have the file into a stream, we
then just use the Response object and download the file via the output stream.
The real power in this snippet is in the lines above; by adding a header, you are telling the browser to download the file as an attachment.
Then you set the ContentType header which is added, and set your MIME type so that the browser knows what kind of file it is about
to download. You can choose any of the following MIME types for the browser:
".asf" = "video/x-ms-asf"
".avi" = "video/avi"
".doc" = "application/msword"
".zip" = "application/zip"
".xls" = "application/vnd.ms-excel"
".gif" = "image/gif"
".jpg"= "image/jpeg"
".wav" = "audio/wav"
".mp3" = "audio/mpeg3"
".mpg" "mpeg" = "video/mpeg"
".rtf" = "application/rtf"
".htm", "html" = "text/html"
".asp" = "text/asp"
A full example of how to go about downloading a text file would like the code below:
C#
Hide Copy Code
2 of 8
System.IO.FileStream fs = null;
fs = System.IO.File.Open(Server.MapPath("TextFiles/" +
sFileName + ".txt"), System.IO.FileMode.Open);
byte[] btFile = new byte[fs.Length];
fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
fs.Close();
Response.AddHeader("Content-disposition", "attachment; filename=" +
sGenName);
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(btFile);
Response.End();
}
VB.NET
Hide Copy Code
fs = System.IO.File.Open(Server.MapPath("TextFiles/" + strFileName + _
".txt"), System.IO.FileMode.Open)
Dim btFile(fs.Length) As Byte
fs.Read(btFile, 0, fs.Length)
fs.Close()
With Response
.AddHeader("Content-disposition", "attachment;filename=" & strFriendlyName)
.ContentType = "application/octet-stream"
.BinaryWrite(btFile)
.End()
end with
Conclusion
Using this approach, you should be able to download all types files on Windows systems, but there are some issues with Macintosh
systems. Specifically, you may not be able to download files, instead they will always open up in the browser as expected.
References
Downloading and uploading files
Send an attachment to the browser
License
This article, along with any associated source code and files, is licensed under
The Code Project Open License (CPOL)
Share
3 of 8
Gary Woodfine is a freelance software developer based in Swindon, Wiltshire, UK. Experienced in
Financial Services, Security and Utilities covering all areas of software development including
Solution Architecture, software design and product development. Expertise in full stack software
development.
Search Comments
I'm writting only to alert who will try this with update panel.
4 of 8
And a way that works to me, was create another webform without updatePanel, and call this other page to execute the download
function.
Sign In · View Thread
How to apply Styles to word file in asp.net Member 11432166 16-Jun-15 2:04
Hi,
Can anyone tell me how to apply styles to the word document in asp.net using C#(create and download is working finely.)
Sign In · View Thread
How to apply Styles to word file from web page Member 11432166 16-Jun-15 2:03
Hi,
Can anyone tell me how to apply styles to the word document in asp.net using C#(create and download is working finely.)
Sign In · View Thread
Sign In · View Thread
Sign In · View Thread
Sign In · View Thread
دﻣﺖ ﮔﺮم
ﺧﻴﻠﻲ ﺣﺎل دادي
Sign In · View Thread
This article really helped me. I just wanted to write a string out to the client as an attached file. This taught me everything I
needed to do it.
Sign In · View Thread
Kind Regards,
Gary
5 of 8
Sign In · View Thread
Sign In · View Thread
hi.. i have save the word file using vb.net let say filename is "my examply [my file].doc" ..
then i download the file using vb.net..
the file is downloaded
but the problem is that when i download the file the in the filename the underscore (_) is appended whenever there is space like
"my_example_[my_file].doc"
trimp
Sign In · View Thread
Hi When the open dialog box shows up, there is a default file name in the TextBox,
I want to change it in my code,
how can I do this?
thank you.
Sign In · View Thread
Kind Regards,
Gary
Sign In · View Thread
as more details:
When a client downloads the files, the files all will be saved with name of the page!
6 of 8
(for example: file on server is named: a.doc and my page is named xyz.aspx, the file is saved on client as xyz.doc)
I want files to be saved with their own names.
thanks
Sign In · View Thread
what are you supplying as the file name you are downloading
Kind Regards,
Gary
Sign In · View Thread
Hi,
thank you so much.
As an example, to download a doc file to client I wrote this:
Hide Copy Code
<br />
<br />
Response.AddHeader("Content-disposition", @"attachment; filename=~\XYZ.doc");<br />
Response.AddHeader("content-type", "application/msword");<br />
Response.WriteFile(@"~\XYZ.doc", false);<br />
Response.End();<br />
Sign In · View Thread
Cool,
Thanks.
I will try update the article at some point to show the simplified methods
Kind Regards,
Gary
Sign In · View Thread
You can simplify this example because you don't need write de Textbox to a file first:
Greetings.
Sign In · View Thread
Sure you are correct, but if you read the article I actually say you don't need to do that, but I left that code in to illustrate
how to actually create a text file.
Kind Regards,
Gary
Sign In · View Thread
How can i do the same in VB.NET 1.1 console application i.e. how can i set the MIME type for every extension and render it to an
HTML page?
Sign In · View Thread
you would need to import the System.Web Namepspace into the Console App.
Kind Regards,
Gary
Sign In · View Thread
With Response
.AddHeader("Content-disposition", "attachment;filename=" & oAtmt.FileName)
.ContentType = "application/octet-stream"
.BinaryWrite(btFile)
.End()
8 of 8
End With
Sign In · View Thread
what i m doing is saving the attachments on my local disk and reading it from there using streamreader.
At this place can i specify the MIME type and display it in HTML page ?If yes then How?
Sign In · View Thread
Kind Regards,
Gary
Sign In · View Thread
Reynaldo Ferrer
Sign In · View Thread
Last Visit: 7-Dec-18 8:55 Last Update: 7-Dec-18 8:55 Refresh 1 2 Next »
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Layout: Article Copyright 2006 by Three Nine Consulting
Select Language ▼
Web04 | 2.8.181205.1 | Last Updated 12 Feb 2007 fixed | Everything else Copyright © CodeProject, 1999-2018
fluid