gpt4 book ai didi

c# - 创建文本文件并下载

转载 作者:IT王子 更新时间:2023-10-29 04:19:00 25 4
gpt4 key购买 nike

我正在尝试写入内存中的文本文件,然后下载该文件而不将文件保存到硬盘。我正在使用 StringWriter 来写入内容:

StringWriter oStringWriter = new StringWriter();
oStringWriter.Write("This is the content");

我该如何下载这个文件?

编辑:答案的组合给了我我的解决方案。在这里:

StringWriter oStringWriter = new StringWriter();
oStringWriter.WriteLine("Line 1");
Response.ContentType = "text/plain";

Response.AddHeader("content-disposition", "attachment;filename=" + string.Format("members-{0}.csv",string.Format("{0:ddMMyyyy}",DateTime.Today)));
Response.Clear();

using (StreamWriter writer = new StreamWriter(Response.OutputStream, Encoding.UTF8))
{
writer.Write(oStringWriter.ToString());
}
Response.End();

最佳答案

这为我解决了:

        MemoryStream ms = new MemoryStream();
TextWriter tw = new StreamWriter(ms);
tw.WriteLine("Line 1");
tw.WriteLine("Line 2");
tw.WriteLine("Line 3");
tw.Flush();
byte[] bytes = ms.ToArray();
ms.Close();

Response.Clear();
Response.ContentType = "application/force-download";
Response.AddHeader("content-disposition", "attachment; filename=file.txt");
Response.BinaryWrite(bytes);
Response.End();

关于c# - 创建文本文件并下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4570433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com