Reading a binary file and using Response.BinaryWrite()

后端 未结 10 1928
既然无缘
既然无缘 2020-11-28 12:16

I have an app that needs to read a PDF file from the file system and then write it out to the user. The PDF is 183KB and seems to work perfectly. When I use the code at th

10条回答
  •  北海茫月
    2020-11-28 12:22

    Just for future reference, as stated in this blog post: http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx

    It is not recommended to call Response.Close() or Response.End() - instead use CompleteRequest().

    Your code would look somewhat like this:

        byte[] bytes = {};
    
        bytes = GetBytesFromDB();  // I use a similar way to get pdf data from my DB
    
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = true;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/pdf";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + anhangTitel);
        Response.AppendHeader("Content-Length", bytes.Length.ToString());
        this.Context.ApplicationInstance.CompleteRequest();
    

提交回复
热议问题