1

将文件流式传输到浏览器时出现错误,但仅在第二次到 n 次。第一次它工作正常。

C:\Users\gfinzer\AppData\Local\Temp\BTnADAkZ.pdf. could not be saved because the source file could not be read

这是代码:

protected void Page_Load(object sender, EventArgs e)
{
    //Get the parameters
    string reportName = Utils.ParseStringRequest(Request, "reportName") ?? string.Empty;
    string reportGuid = Session["reportGuid"].ToString();
    string path = Path.Combine(ReportPath(), Utils.GetSessionReportName(reportName, reportGuid));

    using (var fileStream = File.Open(path, FileMode.Open))
    {
        Response.ClearHeaders();
        Response.Clear();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + reportName + "\"");
        Response.AddHeader("Content-Length", fileStream.Length.ToString(CultureInfo.InvariantCulture));
        StreamHelper.CopyStream(fileStream, Response.OutputStream);
        Response.Flush();
    }

}

这是复制流:

    public static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[32768];
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, read);
        }
    }
4

1 回答 1

1

我可能是错的,但你需要在Response.End()之后打电话Flush吗?

于 2012-09-11T15:01:29.230 回答