2

我使用流编写器在我的代码中生成文本文件,该流编写器使用 mvc 格式用于 Web 应用程序。但是我的文本文件没有下载我的页面?我的示例代码如下:

// 文件具有流 writerformat 中的所有值

return File(file, "text/plain", "Export.txt");
4

2 回答 2

1

尝试在您返回之前添加此内容:

var cd = new System.Net.Mime.ContentDisposition
{
    FileName = "Export.txt", 
    Inline = false, 
};
Response.AppendHeader("Content-Disposition", cd.ToString());

return File(file, "text/plain");
于 2013-04-12T13:00:27.163 回答
0

您可以调用一个方法,该方法将为您提供如下文件流:

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"
        };
}
于 2013-04-12T13:01:54.687 回答