Skip to content

Instantly share code, notes, and snippets.

@jopfre
Last active October 25, 2018 06:16
Show Gist options
  • Save jopfre/304078ea0bf317bc96ef to your computer and use it in GitHub Desktop.
Save jopfre/304078ea0bf317bc96ef to your computer and use it in GitHub Desktop.
force download of files server and client side
#server side force download of files ending with 'download.pdf'
<FilesMatch "download\.pdf$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
//client side, ie and ios opens in new tab
var IE = /*@cc_on!@*/false || !!document.documentMode;
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
//on useraction e.g. click/submit
if(IE || iOS) {
window.open('https://info.ensono.com/rs/999-OHA-317/images/NA-AssessReport-20180810.pdf', '_blank');
}
//anytime
var a = document.createElement('a');
a.href = "download.pdf"; //same origin
a.setAttribute("download", "");
a.setAttribute('target', '_blank');
document.body.appendChild(a);
a.click();
location ~* download\.pdf$ {
types { application/octet-stream .pdf; }
default_type application/octet-stream;
add_header Content-Disposition attachment;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment