我要检查文件(例如.pdf文件)是否下载成功。
public class TestController : Controller
{
public FilePathResult Index()
{
return File("path/to/file", "application/pdf");
}
}
我能够找到此解决方案:
public class CheckedFileStreamResult : FileStreamResult
{
public CheckedFileStreamResult(FileStream stream, string contentType)
:base(stream, contentType)
{
DownloadCompleted = false;
}
public bool DownloadCompleted { get; set; }
protected override void WriteFile(HttpResponseBase response)
{
var outputStream = response.OutputStream;
using (FileStream)
{
var buffer = new byte[_bufferSize];
var count = FileStream.Read(buffer, 0, _bufferSize);
while(count != 0 && response.IsClientConnected)
{
outputStream.Write(buffer, 0, count);
count = FileStream.Read(buffer, 0, _bufferSize);
}
DownloadCompleted = response.IsClientConnected;
}
}
private const int _bufferSize = 0x1000;
}
我不知道如何在 OnResultExecuted 处理程序中检查 DownloadCompleted 标志。该操作方法返回 FilePathResult 。请指导我如何检查 OnResultExecuted 处理程序中的 DownloadCompleted 标志。我需要打字吗?