Creating a text file on the fly and have it download/save on client side

大兔子大兔子 提交于 2019-12-06 10:22:35
MemoryReader mr = new MemoryStream();
TextWriter tw = new StreamWriter(mr);

foreach (string s in stringList) {
    tw.WriteLine(s);
}
tw.Flush();

return File(mr, "application/force-download", "myFile.txt")

Or directly write to the response:

HttpContext.Response.Body.WriteAsync(...);

Also viable alternative

TextWriter tw = new StreamWriter(HttpContext.Response.Body);

so you can directly write to the output string, without any additional memory usage. But you shouldn't close it, since it's the connection to the browser and may be needed further up in the pipeline.

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