MVC Controller Using Response Stream

前端 未结 4 566
轮回少年
轮回少年 2020-12-09 12:06

I\'m using MVC 3 I would like to dynamically create a CSV file for download, but I am unsure as to the correct MVC orientated approach.

In conventional ASP.net, I wo

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 12:42

    I spent some time on the similar problem yesterday, and here's how to do it right way:

    public ActionResult CreateReport()
    {
        var reportData = MyGetDataFunction();
        var serverPipe = new AnonymousPipeServerStream(PipeDirection.Out);
        Task.Run(() => 
        {
            using (serverPipe)
            {
                 MyWriteDataToFile(reportData, serverPipe)
            }
        });
    
        var clientPipe = new AnonymousPipeClientStream(PipeDirection.In,
                 serverPipe.ClientSafePipeHandle);
        return new FileStreamResult(clientPipe, "text/csv");
    }
    

提交回复
热议问题