Solved

ASP.Net: Getting "view_pdf.aspx?DocRef=12345" to return a PDF to the user

Posted on 2004-04-02
4
278 Views
Last Modified: 2010-05-18
We have some PDF's that can be viewed by users. Rather than simply providing them with a link to the URL of the document, we would like some way to first check that they have permissions to view that PDF (by cross-referencing the PDF ref with the user ID in a database), then if they do have permissions to view it, return the PDF output to the page. Also, we don't want them to simply be able to guess the URL of other PDF's and be able to view them. Therefore, can somebody please confirm, and provide more code instead of the comments below:

----------
Dim sDocRef As String
Dim bAccess As Boolean
Dim sDocLocation As String

sDocRef = Request.QueryString("DocRef")

' Query the database to check they have access to this file.
' Do SQL stuff.
If myRS("Access") = 1 Then
  ' Query the database to return the real location of the PDF
  ' Do SQL Stuff
  sDocLocation = myRS("Location")
  ' Set the content type of the page to be a PDF *****
  ' Read the contents of the PDF *****
  ' Output the contents of the PDF to the page *****
Else
  Response.Redirect = "permission_denied.aspx"
End If
----------

I.e. by going to "view_pdf?DocRef=12345" they are presented with the PDF in that same window [assuming have permissions].

Can somebody please provide the code where the comment has ***** at the end of it.

Any other bits of code in there would be helpful, e.g. check PDF exists, etc. are there any issues with this method?

Any help much appreciated.

Thanks,

Ben.

0
Comment
Question by:webtechy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:culshaja
culshaja earned 250 total points
ID: 10740851
Response.Clear()
Response.ContentType = "application/pdf"
Dim MyFileStream As FileStream
Dim FileSize As Long
 
 MyFileStream = New FileStream(sDocLocation , FileMode.Open)
 FileSize = MyFileStream.Length
Dim Buffer(CInt(FileSize)) As Byte
 MyFileStream.Read(Buffer, 0, CInt(FileSize))
 MyFileStream.Close()
Response.BinaryWrite(Buffer)
Response.Flush
Response.Close()


James :-)

0
 
LVL 35

Accepted Solution

by:
YZlat earned 250 total points
ID: 10740929
Dim PDFBuffer as Byte

' set the content type to PDF
Response.ContentType = "application/pdf"

' add content type header
Response.AddHeader("Content-Type", "application/pdf")

' set the content disposition
Response.AddHeader("Content-Disposition", "inline;filename=form.pdf")

' write the buffer with pdf file to the output
Response.BinaryWrite(PDFBuffer)

Response.End()
0
 
LVL 2

Author Comment

by:webtechy
ID: 10741990
Cheers, guys. So this method will work? i.e. it doesn't matter to the browser that the extension of the page will be .aspx but we are actually returning a PDF as we are setting the content type? Is this method the "best" for restructing access to PDF's?
0
 
LVL 7

Expert Comment

by:culshaja
ID: 10742050
This is the standard way to do it with any file type.

The content type tells the page what is actually being sent, not the extension. The extension is used by the web server to determine what the content type should be initially set to, but in a server-side environment like ASP/ASP.Net/PHP etc you can chnage this as we do above. Its the MIME type that is sent back as the content type that the client uses.

You can do this with a web page that is just a table as contents and tell it the content type is the MIME type for excel and it will open the web page in Excel with the page contents in the first workbook.

James :-)
0

Featured Post

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question