asp.net-mvc 如何在 ASP.NET MVC 中创建文件并通过 FileResult 返回它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1375486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:49:22  来源:igfitidea点击:

How to create file and return it via FileResult in ASP.NET MVC?

asp.net-mvc

提问by Ante

I have to create and return file in my aplication ASP.net MVC aplication. The file type should be normal .txt file. I know that i can return FileResult but i don't know how to use it.

我必须在我的应用程序 ASP.net MVC 应用程序中创建并返回文件。文件类型应该是普通的 .txt 文件。我知道我可以返回 FileResult 但我不知道如何使用它。

public FilePathResult GetFile()
{
string name = "me.txt";

FileInfo info = new FileInfo(name);
if (!info.Exists)
{
    using (StreamWriter writer = info.CreateText())
    {
        writer.WriteLine("Hello, I am a new text file");

    }
}

return File(name, "text/plain");
}

This code doesn't work. Why? How to do it with stream result?

此代码不起作用。为什么?如何处理流结果?

回答by BigBlondeViking

EDIT ( If you want the stream try this: )

编辑(如果你想要流试试这个:)

public FileStreamResult GetFile()
{
    string name = "me.txt";

    FileInfo info = new FileInfo(name);
    if (!info.Exists)
    {
        using (StreamWriter writer = info.CreateText())
        {
            writer.WriteLine("Hello, I am a new text file");

        }
    }

    return File(info.OpenRead(), "text/plain");

}

You could try something like this..

你可以试试这样的..

public FilePathResult GetFile()
{
    string name = "me.txt";

    FileInfo info = new FileInfo(name);
    if (!info.Exists)
    {
        using (StreamWriter writer = info.CreateText())
        {
            writer.WriteLine("Hello, I am a new text file");

        }
    }

    return File(name, "text/plain");

}

回答by Tomas Aschan

Open the file to a StreamReader, and pass the stream as an argument to the FileResult:

打开文件到 a StreamReader,并将流作为参数传递给 FileResult:

public ActionResult GetFile()
{
    var stream = new StreamReader("thefilepath.txt");
    return File(stream.ReadToEnd(), "text/plain");
}

回答by Bronek

Another example of creating and downloading file from ASP NET MVC application at once but file content is created in memory (RAM) - on the fly:

另一个从 ASP NET MVC 应用程序同时创建和下载文件的示例,但文件内容是在内存 (RAM) 中创建的 - 即时:

public ActionResult GetTextFile()
{
    UTF8Encoding encoding = new UTF8Encoding();
    byte[] contentAsBytes = encoding.GetBytes("this is text content");

    this.HttpContext.Response.ContentType = "text/plain";
    this.HttpContext.Response.AddHeader("Content-Disposition", "filename=" + "text.txt");
    this.HttpContext.Response.Buffer = true;
    this.HttpContext.Response.Clear();
    this.HttpContext.Response.OutputStream.Write(contentAsBytes, 0, contentAsBytes.Length);
    this.HttpContext.Response.OutputStream.Flush();
    this.HttpContext.Response.End();

    return View();
}