当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Reading a binary file and using Response.BinaryWrite()

问题描述:

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 the bottom the browser gets a file 224KB and I get a message from Acrobat Reader saying the file is damaged and cannot be repaired.

Here is my code (I've also tried using File.ReadAllBytes(), but I get the same thing):

using (FileStream fs = File.OpenRead(path))

{

int length = (int)fs.Length;

byte[] buffer;

using (BinaryReader br = new BinaryReader(fs))

{

buffer = br.ReadBytes(length);

}

Response.Clear();

Response.Buffer = true;

Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", Path.GetFileName(path)));

Response.ContentType = "application/" + Path.GetExtension(path).Substring(1);

Response.BinaryWrite(buffer);

}

网友答案:

Try adding

Response.End();

after the call to Response.BinaryWrite().

You may inadvertently be sending other content back after Response.BinaryWrite which may confuse the browser. Response.End will ensure that that the browser only gets what you really intend.

网友答案:
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.Close();
        Response.End();

This works for us. We create PDFs from SQL Reporting Services.

网友答案:

We've used this with a lot of success. WriteFile do to the download for you and a Flush / End at the end to send it all to the client.

            //Use these headers to display a saves as / download
            //Response.ContentType = "application/octet-stream";
            //Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}.pdf", Path.GetFileName(Path)));

            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", String.Format("inline; filename={0}.pdf", Path.GetFileName(Path)));

            Response.WriteFile(path);
            Response.Flush();
            Response.End();
网友答案:

Since you're sending the file directly from your filesystem with no intermediate processing, why not use Response.TransmitFile instead?

Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition",
    "attachment; filename=\"" + Path.GetFileName(path) + "\"");
Response.TransmitFile(path);
Response.End();

(I suspect that your problem is caused by a missing Response.End, meaning that you're sending the rest of your page's content appended to the PDF data.)

网友答案:

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();
网友答案:

Please read this before using Response.TransmitFile: http://improve.dk/blog/2008/03/29/response-transmitfile-close-will-kill-your-application

网友答案:

Maybe you are missing a Response.close to close de Binary Stream

网友答案:

In addition to Igor's Response.Close(), I would add a Response.Flush().

网友答案:

I also found it necessary to add the following:

Response.Encoding = Encoding.Default

If I didn't include this, my JPEG was corrupt and double the size in bytes.

But only if the handler was returning from an ASPX page. It seemed running from an ASHX this was not required.

分享给朋友:
您可能感兴趣的文章:
随机阅读: