I have in my page "downloads.php" the following line:
<a href="#" onclick="location.replace('uploads/ejemplo.pdf');" download>
For privacy issues, I'm using the onclick so that the user doesn't see the original link where the file is hosted, but instead downloads it immediately (I know it's not the best security method).
The problem is that when I click, not only the file "example.pdf" is downloaded, but it also downloads my page "downloads.php" as a file
Is there a method to avoid this? Do you recommend another method to hide the file path?
EDIT
I am using the attribute download
for the pdf files that Chrome displays instead of downloading, thanks for clarifying that if I remove this attribute I eliminate the error by commenting at the beginning
I am going to base myself on any scenario, the closest thing to what you propose would be, for example, a list of files that are offered for download and that, according to you, you want to hide the URL where those files are.
We will put that list in the form of a link, we will listen to the clicks on each one of them and we will launch from the client a request to the server so that the download is done directly, which is what you require.
From the client we are going to launch a request
XMLHttpRequest
to the server, sending it the location of the file. We will indicate that a response of the type is expectedblob
and from the response we will be able to download the file.The file, if it exists, will be downloaded to the client's computer, without showing the resource link at any time.
It is convenient to control the response of the request
XMLHttpRequest
, for example, in case the file does not exist, to make an adequate handling of the code.I have not done so for reasons of brevity.
The code has been tested in a real environment and it works.
I hope it helps you.
JavaScript:
HTML:
It is because of the download attribute that you have put. take it off