Created
March 7, 2019 11:33
-
-
Save kartick14/a2f2dbafb595d2cedac7362040393111 to your computer and use it in GitHub Desktop.
Javascript force file download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button class="js-download-link button" onclick="downloadFile('https://wattswork.s3.amazonaws.com/test-media/video/test_Drop-1280x720-16-9-HD.mp4','dwn.mp4'); return false;">Download MP4 File</button> | |
<button class="js-download-link button" onclick="downloadFile('https://wattswork.s3.amazonaws.com/test-media/PDFs/test-file.pdf','tfile.pdf'); return false;">Download PDF File</button> | |
<button class="js-download-link button" onclick="downloadFile('https://wattswork.s3.amazonaws.com/test-media/Text/test-file.txt','file.txt'); return false;">Download TXT File</button> | |
<script type="text/javascript"> | |
function downloadFile(data, fileName, type="text/plain") { | |
// Create an invisible A element | |
const a = document.createElement("a"); | |
a.style.display = "none"; | |
document.body.appendChild(a); | |
// Set the HREF to a Blob representation of the data to be downloaded | |
a.href = window.URL.createObjectURL( | |
new Blob([data], { type }) | |
); | |
// Use download attribute to set set desired file name | |
a.setAttribute("download", fileName); | |
// Trigger the download by simulating click | |
a.click(); | |
// Cleanup | |
window.URL.revokeObjectURL(a.href); | |
document.body.removeChild(a); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment