Skip to content
Advertisement

Prevent download of PDF file from site

I have files stored in the server with the Plesk panel, currently, any person with the link of my site can access the file and download it.

Is there any way to prevent the download for all the users?

Advertisement

Answer

The general approach is to create a folder that is not in the root folder of the web site. In other words, you don’t allow nor have valid URL’s to a folder as a result. This does mean then you have to build some type of web page that can say list out some files and a button to click to download such files.

In other words, no valid url’s exist. This tends to suggest that you have some type of grid or display of files. often even a database table to drive such a display is used.

When the user clicks on say a button on that grid row, then code behind can fetch the file, and “stream” it down to the user. The end result is thus no valid url’s or resolvable path to the files exists.

Also, from IIS, turn off directory browsing. and thus again no valid URL’s to the files exists.

The other approach? You can build a custom http handler. This approach is quite common. That way, any url that ends in .pdf will be trapped, and thus not allowed. As noted, this again means you have to provide a “list” of files, and a button to download – and again you can then stream the file from the server.

So there is quite a few ways, but ultimate, I as a general rule don’t allow files to be downloaded or even accessed by URL’s. I always provide some kind of web page, and a list of choices for the user. The code behind is what actually fetches the file, and then streams it down to the browser.

So, you can Search for how to build a custom http handler for pdf files in asp.net

Search for how to stream a file to users in asp.net

how to turn off directory browsing in IIS.

There are a truckload of options here – in fact too many to really post and explain in Stack Overflow answer.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement