David Ferry
David Ferry

Reputation: 293

how to export the text file in mvc?

I generate the text file in my code using the stream writer using mvc format for web application . But my text file not download the my page? my sample code is below:

// file have the all values in stream writerformat

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

Upvotes: 2

Views: 2832

Answers (2)

von v.
von v.

Reputation: 17108

You can call a method that will give you your file stream like this:

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"
        };
}

Upvotes: 0

Z .
Z .

Reputation: 12837

try adding this before your return:

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

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

Upvotes: 1

Related Questions