How to force download a file with JavaScript

Developer
Size
2,272 Kb
Views
26,312

How do I make an how to force download a file with javascript?

What is a how to force download a file with javascript? How do you make a how to force download a file with javascript? This script and codes were developed by Jelmer on 02 December 2022, Friday.

How to force download a file with JavaScript Previews

How to force download a file with JavaScript - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>How to force download a file with JavaScript</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <script src="js/index.js"></script>
</body>
</html>

How to force download a file with JavaScript - Script Codes CSS Codes

/* How to force download a file. */

How to force download a file with JavaScript - Script Codes JS Codes

setTimeout(function() { url = '//assets.codepen.io/images/codepen-logo.svg'; // downloadFile(url); // UNCOMMENT THIS LINE TO MAKE IT WORK
}, 2000);
// Source: http://pixelscommander.com/en/javascript/javascript-file-download-ignore-content-type/
window.downloadFile = function (sUrl) { //iOS devices do not support downloading. We have to inform user about this. if (/(iP)/g.test(navigator.userAgent)) { //alert('Your device does not support files downloading. Please try again in desktop browser.'); window.open(sUrl, '_blank'); return false; } //If in Chrome or Safari - download via virtual link click if (window.downloadFile.isChrome || window.downloadFile.isSafari) { //Creating new link node. var link = document.createElement('a'); link.href = sUrl; link.setAttribute('target','_blank'); if (link.download !== undefined) { //Set HTML5 download attribute. This will prevent file from opening if supported. var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length); link.download = fileName; } //Dispatching click event. if (document.createEvent) { var e = document.createEvent('MouseEvents'); e.initEvent('click', true, true); link.dispatchEvent(e); return true; } } // Force file download (whether supported by server). if (sUrl.indexOf('?') === -1) { sUrl += '?download'; } window.open(sUrl, '_blank'); return true;
}
window.downloadFile.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
window.downloadFile.isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;
How to force download a file with JavaScript - Script Codes
How to force download a file with JavaScript - Script Codes
Home Page Home
Developer Jelmer
Username jelmerdemaat
Uploaded December 02, 2022
Rating 3
Size 2,272 Kb
Views 26,312
Do you need developer help for How to force download a file with JavaScript?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Jelmer (jelmerdemaat) Script Codes
Create amazing blog posts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!