gpt4 book ai didi

c# - 网络核心 : how to make the browser show file as being downloaded before it's sent

转载 作者:行者123 更新时间:2023-11-30 12:54:53 25 4
gpt4 key购买 nike

我有以下生成文件的操作:

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 发送带有文件名的 header (以便浏览器向用户显示文件已开始下载)并在生成文件内容时延迟发送正文。可以这样做吗?

我尝试了以下方法:

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 个字节用于发送 header ,我会很高兴。

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

最佳答案

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

但是,如果您想写类似的东西,请考虑阅读 Stephen Cleary's post一些有用的抽象。

关于c# - 网络核心 : how to make the browser show file as being downloaded before it's sent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52867402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com