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

[亡魂溺海] 提交于 2020-12-01 10:04:10

问题


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.


回答1:


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



回答2:


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



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



回答4:


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


来源:https://stackoverflow.com/questions/53491070/create-text-file-and-download-without-saving-on-server-in-asp-net-core-mvc-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!