public IActionResult ExportCod3es(string codes)
{
try
{
byte[] bs = { };
if (string.IsNullOrEmpty(codes))
return File(bs, "text/xml", "导出出错.txt");
var filename = DateTime.Now.ToString("yyyyMMddHHmmss") + $"PC退货.txt";
using (MemoryStream mem = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(mem))
{
sw.BaseStream.Seek(0, SeekOrigin.End);
var list = codes.Split(',').ToList();
//开始写入
foreach (var item in list)
{
sw.WriteLine(item);
}
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
}
return File(mem.ToArray(), "text/xml", filename);
}
}
catch (Exception ex)
{
byte[] bs = { };
return File(bs, "text/xml", "导出出错.txt");
}
}
参考:https://www.cnblogs.com/ShenJA/p/12580622.html