Content-Disposition inline not able to download PDF when open in chrome (asp.net)

587 Views Asked by At
byte[] byteData= GetByteData(this.reportContent);
response.Clear();
response.ClearContent();
response.ClearHeaders();
 
response.Buffer = true;
response.AddHeader("Accept-Header", byteData.Length.ToString());
response.ContentType ="application/pdf";
response.Charset = "utf-8";
response.AddHeader("Accept-Ranges", "bytes");
string cd = $"inline;filename=myReport-123.pdf";
response.AddHeader("Content-Disposition", cd.ToString().Trim());
response.AddHeader("Content-Length", byteData.Length.ToString());
if (byteData.Length > 0)
    response.BinaryWrite(byteData);

response.Flush();
response.Close();

The above code is not working and the file downloads as Report.aspx instead of myReport-123.pdf I have tried almost everything but did not work. When I run the app like localhost://........report.aspx then its working fine but when I open like my IP https://192.168.1.1/...report.aspx its not working. This issue is only on Chrome and IE and Edge not in firefox. ALso the fileName.pdf is changed to report.aspx if I pass MYFILENAMESHOULDNOTCHANGE.pdf

I tried to add **response.AddHeader("Content-Transfer-Encoding", "Binary");
I also tried to other available options on internet but could not get the desired details.

The above code is not working and the file downloads as Report.aspx instead of myReport-123.pdf I have tried almost everything but did not work. When I run the app like localhost://........report.aspx then its working fine but when I open like my IP https://192.168.1.1/...report.aspx its not working. This issue is only on Chrome and IE and Edge not in firefox.**

1

There are 1 best solutions below

3
Albert D. Kallal On

Well, first and MOST important?

the raw binary data stream you send to the client side MUST be a 100% perfect binary formatted PDF file, and in the Adobe PDF format.

In other words, you can't just transmit a word file, or even some simple markup, and expect the other end to by some act of magic format and convert the markup into a valid PDF binary document, and one that conforms to the Adobe PDF standards.

So, for example, this code will send/download a valid PDF file.

    protected void cmdDownLoad_Click(object sender, EventArgs e)
    {

        string sFileWithPath = Server.MapPath(@"~/Content/CONTROL.pdf");

        if (File.Exists(sFileWithPath))
        {
            string sFileNameOnly = Path.GetFileName(sFileWithPath);
            string sMineType = MimeMapping.GetMimeMapping(sFileWithPath);
            FileInfo sFinfo = new FileInfo(sFileWithPath); // get lengh of file

            Response.Clear();
            Response.ContentType = sMineType;
            Response.AppendHeader("Content-Length", sFinfo.Length.ToString());

            Response.AppendHeader("Content-Disposition", "attachment; filename=" + sFileNameOnly);
            Response.TransmitFile(sFileWithPath);
            Response.End();


        }
    }

You don't show, nor explain where, and how you created a valid PDF file, and your source looks to be:

this.reportContent

So, is the above 100% valid PDF document?

You can NOT just send same some markup, but BEOFRE you transmit the content in question, that content has to be a correct binary formatted PDF document.

Now, if reportContent is in fact a binary PDF document, and not some markup, then in theory, the above posted I code used should work. In other words, you can replace the PDF document with the binary stream - a valid PDF one, and that should also work.

So, above could say become this:

        string sFileWithPath = Server.MapPath(@"~/Content/CONTROL.pdf");
        string sMineType = MimeMapping.GetMimeMapping(sFileWithPath);
        byte[] pDFData = File.ReadAllBytes(sFileWithPath);

        string sFileNameOnly = Path.GetFileName(sFileWithPath);
        Response.Clear();
        Response.ContentType = sMineType;
        Response.AppendHeader("Content-Length", pDFData.Length.ToString());
        Response.AddHeader("Content-Disposition", $"attachment; filename={sFileNameOnly}");
        Response.BinaryWrite(pDFData);
        Response.Flush();
        Response.End();

Again, note VERY close - the resulting output MUST be a legal and correctly formatted binary PDF file.

You can't for example just send some HTML markup here, you have to take the content (this.reportContent), and convert to a valid PDF binary file format BEFORE you send/transmit whatever you have in reportContent.

In other words, reportContent can't be markup - it HAS to be valid binary PDF format byte stream.

So, as a test, write out the content of this.reportContent to a local .pdf file, and see if a PDF viewer can open that file as a PDF.

Edit: Test if you have a valid PDF file

So, to test if you have a valid PDF, then on your dev machine, try this code:

    protected void Button2_Click(object sender, EventArgs e)
    {
       byte[] byteData= GetByteData(this.reportContent);

        string sTestFile = @"c:\test\test.pdf";

        File.WriteAllBytes(sTestFile, byteData);


    }

Now, after you run above code, then try to open the test.pdf file with a PDF viewer. Does the file open correctly?

So, before you test ANY thing else, try ANY other road or code or approach?

You MUST verify that the above output is a 100% valid and working pdf file.

So, try the above, can you open the resulting PDF file with a pdf viewer?