Click here to Skip to main content
15,888,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, 

I have a pdf file of type byte array. I need to download / open the same as pdf in clients machine. 
I tried to achieve the same by adding response headers for content-disposition but not successful. In browser I see something like bytes.

I am not getting any clue of what is happening here. Do anyone has idea on how to achieve this. Thanks in advance!

In Browser I see like:

<pre>%PDF-1.6 %���� 1 0 obj << /Filter/FlateDecode /Length 62778 /Subtype/OpenType >> stream H�\�t���M<y��X�i|ӂi���Ǵ�:�n��X� C�����=�u�ĭ�`8@4k���R@��F�z܌��N�9�f3 rB�>�D��r���gq����<�(@D)m6#Z�܌��CɨfZ�ܮV��<ҵ�Z�}��1���]]]S<�G�{��/w�@����0P�z�>�?Ŏ�A1(��<f�9�� �A��V�;�փ����8.���� w y���D����H=�iEV



What I have tried:

Code I Tried:

                   //Here I am writing the bytes as File and store in the path - It is successful

                    byte[] bytes;
                    MemoryStream ms = new MemoryStream(pdfFile); //pdfFile is of type Byte[]
                    bytes = ms.ToArray();
                    File.WriteAllBytes(@"C:\inetpub\wwwroot\s92.dev.local\TestForm.pdf", bytes);

                  //Here I am trying to download the file - It is not successful, I am seeing somehting like bytes as shown in image above
                    var response = HttpContext.Current.Response;
                    response.Clear();
                    response.ClearHeaders();
                    response.ClearContent();
                    response.ContentType = "application/pdf";
                    response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.pdf");
                    response.TransmitFile(HttpContext.Current.Server.MapPath("~/TestForm.pdf"));
                    response.End();
Posted
Updated 3-Aug-21 3:26am
v2

1 solution

That's some very confused code: you have a byte array, so you copy it to a MemoryStream, then copy the MemoryStream to another byte array, and then write that third copy of the data to a local file, before sending the local file back to the client.

You could simply write the original byte array to the file before sending it:
C#
string path = Server.MapPath("~/TestForm.pdf");
File.WriteAllBytes(path, pdfFile);

However, remember that multiple users could be accessing your site at the same time. If two users request a PDF download at the same time, you will be overwriting the file for one user with the data for another. This could lead to corrupted downloads, or to your site exposing confidential information to unauthorized users.

A better solution would be to simply write the byte array directly to the response, bypassing the file system.
C#
response.Clear();
response.ContentType = "application/pdf";
response.AppendHeader("Content-Disposition", "attachment; filename=\"MyFile.pdf\"");
response.OutputStream.Write(pdfFile);
response.End();
 
Share this answer
 
Comments
Indhu S 2021 3-Aug-21 10:29am    
Thanks much for the response. Sorry for the confusion, the first block I just tried to see whether it is at least getting saved in file system. Also I unnecessarily converting the types back and forth.

Leaving that part, I tried to directly write the byte array by following your code

var response = HttpContext.Current.Response;
response.Clear();
response.ContentType = "application/pdf";
response.AppendHeader("Content-Disposition", "attachment; filename=\"MyFile.pdf\"");
response.OutputStream.Write(pdfFile, 0, pdfFile.Length);
response.End();


But my browser output is still the same. I also tried with response.BinaryWrite(pdfFile) but no luck..

Can you share some light?
Richard Deeming 3-Aug-21 11:21am    
That would suggest your Content-Disposition and Content-Type headers aren't reaching the browser, or the browser is ignoring them for some reason.

Check the network request in your browser's developer tools to make sure the headers are as expected.
Indhu S 2021 4-Aug-21 11:52am    
Thanks for the response Richard!
I am seeing the response headers as given below in network tab though. So really not sure what is going wrong.

cache-control: private
content-disposition: attachment; filename="MyFile.pdf"
content-type: application/pdf
date: Wed, 04 Aug 2021 15:40:19 GMT
server: Microsoft-IIS/10.0
Richard Deeming 4-Aug-21 12:35pm    
The headers look correct. There must be something odd with your browser. Can you try a different browser?
Indhu S 2021 5-Aug-21 2:01am    
Thanks again for your response. Yes, I tried that already and could see the same behavior across all browser(Chrome/Firefox and Edge) :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900