HoganHost blog

[email protected]

FORCE A FILE OR MP3 LINK TO DOWNLOAD WHEN LINK IS CLICKED

So here’s a simple snippet for when you want to force a download of a file (such as a PDF, .doc etc), when a link is clicked.

The default action will open the document in the either the same browser window, or in a new tab/window by using the usual target methods:

OPEN FILE IN SAME WINDOW:

<a href="myfile.pdf" target="_self">Click to Download</a>

OPEN FILE IN NEW WINDOW:

<a href="myfile.pdf" target="_blank">Click to Download</a>

 

However, if you want to force the file to download, by prompting a download pop-up box (to open or save), then all you need to do is add ‘download’ to the <a> link as seen below:

FORCE FILE DOWNLOAD WINDOW:

<a href="myfile.pdf" download>Click to Download</a>

EDIT: There appears to be a new method now which may work more consistenly across all browsers:

<a href="myfile.pdf" download="myfile.pdf">Click to Download</a>

OR

To force Mp3 and Video to Download , kindly follow the instruction below!!

This below step is mostly for word-press users, if you are not using wordpress you can has well try the other method

  1. Login to Cpanel

       2. Click on File Manager

 

3. Click on Public Html

 

4. Click on setting at the top right hand side of cpanel (Check the image below)

 

5. tick the box (show hidden file) and save it,exactly showing in the image!!

6. Right click on .httaccess to open the file

7. copy the below code and paste it below # End WordPress and save it

<FilesMatch ".(?i:mp3)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
<FilesMatch ".(?i:mp4)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
<FilesMatch ".(?i:mpg)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
<FilesMatch ".(?i:mov)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
<FilesMatch ".(?i:flv)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>

 

Hola! Hope this helps, if you have any problem kindly drop comment below!!!

3 thoughts on “FORCE A FILE OR MP3 LINK TO DOWNLOAD WHEN LINK IS CLICKED”

  1. Thanks alot man. But does this code gets expires? I’m asking based on what I discovered the code gets expired and you have it add it back.

Leave a Comment

Your email address will not be published.