我使用流编写器在我的代码中生成文本文件,该流编写器使用 mvc 格式用于 Web 应用程序。但是我的文本文件没有下载我的页面?我的示例代码如下:
// 文件具有流 writerformat 中的所有值
return File(file, "text/plain", "Export.txt");
我使用流编写器在我的代码中生成文本文件,该流编写器使用 mvc 格式用于 Web 应用程序。但是我的文本文件没有下载我的页面?我的示例代码如下:
// 文件具有流 writerformat 中的所有值
return File(file, "text/plain", "Export.txt");
尝试在您返回之前添加此内容:
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "Export.txt",
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(file, "text/plain");
您可以调用一个方法,该方法将为您提供如下文件流:
public FileStreamResult DownloadFile()
{
//
// This part is where you build your file
//
// file should be a Stream
return new FileStreamResult(file, "text/plain")
{
FileDownloadName = "optional_name_that_you_can_give_your_file"
};
}