我需要检查文件是否下载成功。我的行动结果返回了CheckedFileStreamResult
。但是,OnResultExecutedhandler
甚至在文件下载完成之前就已执行。
我在程序中使用以下建议的解决方案:
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;
response.BufferOutput = false;
using (FileStream fs = new FileStream("D:\\ABC.pdf", FileMode.Open, FileAccess.Read))
{
var buffer = new byte[_bufferSize];
var count = fs.Read(buffer, 0, _bufferSize);
while (count != 0 && response.IsClientConnected)
{
outputStream.Write(buffer, 0, count);
response.Flush();
count = fs.Read(buffer, 0, _bufferSize);
}
DownloadCompleted = response.IsClientConnected;
}
}
private const int _bufferSize = 0x200;
}
使用这种机制无法达到预期的效果。在完全下载文件之前执行OnResultExecuted
处理程序,并且DownloadCompleted
标志给出错误的印象。请指导我如何进行。