15

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.

4 Answers 4

23

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");
Sign up to request clarification or add additional context in comments.

Comments

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");
}

1 Comment

Please give context and explain why and how your code sovles the problem.
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");
}

Comments

2
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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.