I have ASP.NET code that retrieves a PDF file from a database and gives it to the user to download in their browser. I want to make the PDF render inside the browser, without them having to manually open the downloaded file. What’s the best approach to do that? My current code is below for reference.
Response.Clear()
Response.AddHeader("Content-Length", fileContents.Length.ToString())
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename)
Response.OutputStream.Write(fileContents, 0, fileContents.Length)
Response.Flush()
Response.End()
2
Answers
I ended up solving this by using a server-side tag. In my code, I save the PDF to the web server, then change the src attribute to point to that saved location.
With this code, the PDF file will be rendered inside the browser when the user clicks the download link, instead of prompting them to download it. Note that this approach requires that the user has a PDF viewer plugin installed in their browser. If they don’t, they may still be prompted to download the file.