Asp.Net Core:如何使浏览器在发送文件之前将其显示为已下载

时间:2018-10-18 05:16:32

标签: c# asp.net asp.net-core httpresponse

我有以下操作正在生成文件:

public async Task<IActionResult> GetFile()
{
    //some long running action to actually generate the content 
    //(e.g. getting data from DB for reporting)
    await Task.Delay(10000); 

    return File(new byte[]{}, "application/octet-stream");
}

问题在于,当用户点击该链接时,点击操作与浏览器将文件显示为下载状态之间存在很长的延迟。

我真正想要的是让ASP.Net发送带有文件名的标头(以便浏览器将向用户显示文件已开始下载),并在生成文件内容时延迟发送正文。可以这样做吗?

我尝试了以下操作:

public async Task GetFile()
{
    HttpResponse response = this.HttpContext.Response;
    response.ContentType = "application/octet-stream";
    response.Headers["Content-Disposition"] = "inline; filename=\"Report.txt\"";

    //without writing the first 8 bytes 
    //Chrome doesn't display the file as being downloaded 
    //(or ASP.Net doesn't actually send the headers). 
    //The problem is, I don't know the first 8 bytes of the file :)            
    await response.WriteAsync(string.Concat(Enumerable.Repeat("1", 8)));

    await response.Body.FlushAsync();

    await Task.Delay(10000);
    await response.WriteAsync("1234567890");
}

上面的代码有效,如果我不必response.WriteAsync前8个字节发送标头,我会对此感到满意。

还有其他方法可以克服吗?

1 个答案:

答案 0 :(得分:0)

实际上,有问题的代码可以正常工作。 问题是,Chrome doesn't show file as being downloaded until 8 bytes are sent

但是,如果您想编写类似的内容,请考虑阅读Stephen Cleary's post几个有用的抽象。