Disable directlinking to files.

  • Thread starter Thread starter Tiscali Nieuws
  • Start date Start date
T

Tiscali Nieuws

I have a pdf-file, but don't want someone to link to this file from outsite
my own website.

Is it possible to prevent this with ASP?

Thanks, Johan
 
You can provide links that point instead to a page that streams the PDF to
the browser using an ID to pick the PDF, so there was no direct access. At
least that way you could restrict access using some criteria you were happy
with.

--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
 
To expand further on John's suggestion, here is some sample code I use to
retrieve an ID from a database, and stream a file which is in a directory
that isn't even accessible via the web...

------ BEGIN CODE ------
Dim fileType As String

Dim fileID As Integer

If Request.QueryString("type") Is Nothing _

Or Request.QueryString("id") Is Nothing Then

ShowNotFound()

End If

fileType = Request.QueryString("type").ToLower

fileID = Integer.Parse(Request.QueryString("id"))

Select Case fileType

Case "a"

'File requested is an attachment.

Dim fileData As DataSet

Dim inputFileName As String

Dim outputFileName As String

Dim fStream As FileStream

Dim reader As BinaryReader

ArticleManager.SetConnection(AppSettings("Database"))

fileData = ArticleManager.GetAttachmentInfo(fileID)

If fileData.Tables(0).Rows.Count = 0 Then ShowNotFound()

inputFileName = fileData.Tables(0).Rows(0)("AttachmentID")

inputFileName = "KBATT" & inputFileName & ".dat"

outputFileName = fileData.Tables(0).Rows(0)("AttachmentName")

If Not File.Exists(inputFileName) Then ShowNotFound()

fStream = New FileStream(inputFileName, FileMode.Open)

reader = New BinaryReader(fStream)

Response.ContentType = fileData.Tables(0).Rows(0)("AttachmentType")

Response.AddHeader("Content-Disposition", "attachment;filename=" &
outputFileName)

Response.AddHeader("Content-Length", fStream.Length)

Response.BinaryWrite(reader.ReadBytes(fStream.Length))

reader.Close()

Case Else

ShowNotFound()

End Select

Response.End()

End Sub

Private Sub ShowNotFound()

Dim referer As String

Response.Redirect("Error.aspx?code=404")

End Sub

--------END CODE --------


I was minding my own business when John Timney (Microsoft MVP) blurted out:
 
Back
Top