Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create text file and download without saving on server in ASP.net Core MVC 2.1

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.

like image 402
niico Avatar asked Nov 27 '18 00:11

niico


3 Answers

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");
}
like image 162
AddWeb Solution Pvt Ltd Avatar answered Nov 06 '22 17:11

AddWeb Solution Pvt Ltd


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");
like image 19
Chris Pratt Avatar answered Nov 06 '22 17:11

Chris Pratt


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");
}
like image 3
kasptom Avatar answered Nov 06 '22 17:11

kasptom