Filestream.readtimeout:无效的操作异常

时间:2018-12-27 09:06:59

标签: c# asp.net-mvc

我想使用文件流下载文件(以.PDF为例)。这是一个人为的示例:

public ActionResult GetPDF()
{
    ActionResult ar = null;
    FileStream fs = null;
    try
    {
        fs = System.IO.File.OpenRead(filename);
        var bytes = new byte[fs.Length];
        var x = fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
        abcd fr = new abcd(fs, "application/pdf");
    }
    catch(Exception)
    {
    }
    return ar;
}


public class CheckedFileStreamResult : FileStreamResult
{
    public bool DownloadCompleted { get; set; }

    public CheckedFileStreamResult(FileStream stream, string contentType)
        : base(stream, contentType)
    {
        DownloadCompleted = false;
    }

    protected override void WriteFile(HttpResponseBase response)
    {
        var outputStream = response.OutputStream;
        response.BufferOutput = false;
        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;
}

public class abcd : CheckedFileStreamResult
{
    public abcd(FileStream stream, string contentType) : base(stream, contentType)
    {
        DownloadCompleted = false;
    }

    public void Write(HttpResponseBase response)
    {
        WriteFile(response);
    }
}

我总是得到值为0的计数。在这里抛出无效的操作异常: var count = FileStream.Read(buffer, 0, _bufferSize);

请指导我如何进行。我想知道文件是否下载成功。

0 个答案:

没有答案