Reading a binary file and using Response.BinaryWrite()

后端 未结 10 1968
既然无缘
既然无缘 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:30

    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.)

提交回复
热议问题