[英]How to detect file download completion in ASP.NET Core
我的Controller
類中有以下代碼:
[HttpPost]
public async Task<IActionResult> GetDataFile(string id, [FromBody] DataLog log)
{
FileStream fileStream = System.IO.File.OpenRead($"{dataFile}_{id}.log");
//using a using block or Close prevents the download...
return File(fileStream, "application/octet-stream");
}
該文件應該是臨時存在的,因此當客戶端完成下載后, 服務器應刪除該文件 。
我知道當流打開時,我無法關閉它。
我應該使用什么回調? 是否有最佳做法?
您可以執行以下操作:
public FileResult GetDataFile(string id, [FromBody] DataLog log)
{
var bytes = System.IO.File.ReadAllBytes("<absolute path of file here>");
System.IO.File.Delete("<absolute path of file here>");
return File(bytes, "application/octet-stream", "<name of file>");
}
因此,您正在將文件讀入字節數組,然后刪除物理文件並將字節數組返回給客戶端。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.