Skip to main content
March 25, 2016
Question

pdf will not download

  • March 25, 2016
  • 2 replies
  • 395 views

Get error message. aspx is of invalid format. Any help?

This topic has been closed for replies.

2 replies

NKOWA555
Inspiring
March 26, 2016

If you have control of the .aspx source code, then make sure the output buffer is sending the correct MIME type and only sending the PDF data:

Note: Make sure it is only outputting the PDF, and not HTML or anything else:

A VB.NET Example:

Try

     Dim pdf_path_String as string = Server.MapPath("/path/to/document.pdf")

     Dim pdf_file_bytes() as byte = System.IO.File.ReadAllBytes(pdf_path_String)

     'Clears HTML and other stream data

     Response.Clear

     'Set MIME

     Response.ContentType = "application/pdf"

     'Output file

     Response.WriteFile(pdf_path_String)

     'OR output bytes

     'Response.BinaryWrite(pdf_file_bytes())

     ' Call Response.End to ensure nothing else is written to output stream

     Response.End

Catch Ex As Exception

     'NOTE: Important to trap and clear error; because, Response.End throws an exception

     Err.Clear

End Catch

Bernd Alheit
Community Expert
Community Expert
March 26, 2016

How did you download the PDF file?