Solved

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

Posted on 2004-04-02
4
271 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
  • 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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hovering effect 9 29
.Net application crashing 6 20
Check if number is currency 15 29
Redacting a row in Excel based on a term. 17 33
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now