Last active
August 29, 2015 14:04
-
-
Save sms-system/4b3abe7cfa3d9970d847 to your computer and use it in GitHub Desktop.
Скачать програмно файл по ссылке (http://pixelscommander.com/javascript/javascript-file-download-ignore-content-type/)
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
window.downloadFile = function(url,name) { | |
if (window.downloadFile.isChrome || window.downloadFile.isSafari) { | |
var link = document.createElement('a'); | |
link.href = url; | |
if (link.download !== undefined){ | |
var fileName = url.substring(url.lastIndexOf('/') + 1, url.length); | |
link.download = name?name:fileName; | |
} | |
if (document.createEvent) { | |
var e = document.createEvent('MouseEvents'); | |
e.initEvent('click' ,true ,true); | |
link.dispatchEvent(e); | |
return true; | |
} | |
} | |
var query = '?download'; | |
window.open(sUrl + query, '_self'); | |
} | |
window.downloadFile.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1; | |
window.downloadFile.isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment