How to create a text file in asp.net MVC3 using C #

I just want to ask how to generate or create a text file because I want to display my data in the database as text.

im using c # in asp.net mvc 3

Thank you very much! Any answer would be appreciated.

+6
c # asp.net-mvc-3
source share
2 answers

If you just want to return some data from the database to a text file that will be downloaded to the user's local computer, create an Action in the Controller , as in this example:

using System.IO; using System.Text; public class SomeController { // this action will create text file 'your_file_name.txt' with data from // string variable 'string_with_your_data', which will be downloaded by // your browser public FileStreamResult CreateFile() { //todo: add some data from your database into that string: var string_with_your_data = ""; var byteArray = Encoding.ASCII.GetBytes(string_with_your_data); var stream = new MemoryStream(byteArray); return File(stream, "text/plain", "your_file_name.txt"); } } 

then you can create an ActionLink for this action on the View , which will lead to the download of the file:

 @Html.ActionLink("Download Text File", "CreateFile", "SomeController ") 

I hope this helps!

+15
source share

You can return plain text from the action by collecting a string and returning Content(textString, "text/plain") .

+2
source share

All Articles