13

I've found a way to create a text file then instantly download it in the browser without writing it to the server in regular ASP.net:

Create text file and download

The accepted answer uses:

using (StreamWriter writer = new StreamWriter(Response.OutputStream, Encoding.UTF8)) {
  writer.Write("This is the content");
}

I need to do this in ASP.net Core 2.1 MVC - though in that doesn't know what Response.OutputStream is - and I can't find anything on Google to help with that, or other ways to do this.

How can I do this? Thanks.

niico
  • 11,206
  • 23
  • 78
  • 161

4 Answers4

19

If you're dealing with just text, you don't need to do anything special at all. Just return a ContentResult:

return Content("This is some text.", "text/plain");

This works the same for other "text" content types, like CSV:

return Content("foo,bar,baz", "text/csv");

If you're trying to force a download, you can utilize FileResult and simply pass the byte[]:

return File(Encoding.UTF8.GetBytes(text), "text/plain", "foo.txt");

The filename param prompts a Content-Disposition: attachment; filename="foo.txt" header. Alternatively, you can return Content and just set this header manually:

Response.Headers.Add("Content-Disposition", "attachment; filename=\"foo.txt\"");
return Content(text, "text/plain");

Finally, if you're building the text in a stream, then simply return a FileStreamResult:

return File(stream, "text/plain", "foo.txt");
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
9

In below code you use Response.OutputStream. but this is perfactly working in asp.net but Response.OutputStream is throwing error in asp.net core.

using (StreamWriter writer = new StreamWriter(Response.OutputStream, Encoding.UTF8)) {
    writer.Write("This is the content");
}

So, use following code for downloading file in asp.net core.

using (MemoryStream stream = new MemoryStream())
{
StreamWriter objstreamwriter = new StreamWriter(stream);
objstreamwriter.Write("This is the content");
objstreamwriter.Flush();
objstreamwriter.Close(); 
return File(stream.ToArray(), "text/plain", "file.txt");
}
Ortund
  • 8,095
  • 18
  • 71
  • 139
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
3

A little different way but it seems to be what you are looking for

Edit: fixed trailing zeros at the end of the file

[HttpGet]
[Route("testfile")]
public ActionResult TestFile()
{
    MemoryStream memoryStream = new MemoryStream();
    TextWriter tw = new StreamWriter(memoryStream);

    tw.WriteLine("Hello World");
    tw.Flush();

    var length = memoryStream.Length;
    tw.Close();
    var toWrite = new byte[length];
    Array.Copy(memoryStream.GetBuffer(), 0, toWrite, 0, length);

    return File(toWrite, "text/plain", "file.txt");
}

old answer (trailing zeros problem)

[HttpGet]
[Route("testfile")]
public ActionResult GetTestFile() {
    MemoryStream memoryStream = new MemoryStream();
    TextWriter tw = new StreamWriter(memoryStream);

    tw.WriteLine("Hello World");
    tw.Flush();
    tw.Close();

    return File(memoryStream.GetBuffer(), "text/plain", "file.txt");
}
kasptom
  • 2,363
  • 2
  • 16
  • 20
1
public ActionResult Create(Information information)
{
   var byteArray = Encoding.ASCII.GetBytes(information.FirstName + "" + information.Surname + "" + information.DOB + "" + information.Email + " " + information.Tel);
   var stream = new MemoryStream(byteArray);

   return File(stream, "text/plain", "your_file_name.txt");
}
zuluk
  • 1,557
  • 8
  • 29
  • 49
bhav
  • 57
  • 6