admin管理员组文章数量:1356358
"wget .doc" downloads that file to the local disk.
What is the equivalent of the above in javascript? for example, consider the following html snippet.
<html>
<head>
<script language="JavaScript">
function download_file() {
var url = ".doc"
//
// Question:
//
// what should be done here to download
// the file in the url?
//
}
</script>
</head>
<body>
<input type="button" value="Download" onclick="download_file()">
</body>
</html>
Please suggest a solution that is pliant with all the browsers.
Sangeeth.
"wget http://www.example./file.doc" downloads that file to the local disk.
What is the equivalent of the above in javascript? for example, consider the following html snippet.
<html>
<head>
<script language="JavaScript">
function download_file() {
var url = "http://www.example./file.doc"
//
// Question:
//
// what should be done here to download
// the file in the url?
//
}
</script>
</head>
<body>
<input type="button" value="Download" onclick="download_file()">
</body>
</html>
Please suggest a solution that is pliant with all the browsers.
Sangeeth.
Share Improve this question asked Nov 23, 2011 at 2:46 Sangeeth SaravanarajSangeeth Saravanaraj 16.6k21 gold badges71 silver badges98 bronze badges 1- This is answered here: stackoverflow./questions/349067/… – jzila Commented Nov 23, 2011 at 2:50
3 Answers
Reset to default 3After a exploring more than a month, with a help of my friend, we were able to find out the following.
The website where the file is hosted is not allowing us to download the file using window.location = url;
or window.open(url);
Finally we had to use the data-downloadurl
support from HTML5
as follows
<a href="<url-goes-here>" data-downloadurl="audio/mpeg:<filename-goes-here>:<url-goes-here>" download="<filename-goes-here>">Click here to download the file</a>
We embed this html into the host html and when clicked on the link, it triggers the download.
Why not use:
function download_file() {
var url = "http://www.example./file.doc"
window.location = url;
}
See https://developer.mozilla/en/DOM/window.location
If you need to open this in a new window/tab first then use:
function download_file() {
var url = "http://www.example./file.doc"
window.open(url);
}
See https://developer.mozilla/en/DOM/window.open
First thing that always es in mind of every answerer to this question is executing wget shell mand from java script.I'm almost certain that that's not possible because of major security risk.
You pretty much need to have ajax which sends mand to mand line either through php, or another scripting language via ajax...
You could probably make that happen with something like http://www.phantomjs/
I am saying probably because I read it from somewhere.
本文标签: What is the equivalent of wget in javascript to download a file from a given urlStack Overflow
版权声明:本文标题:What is the equivalent of wget in javascript to download a file from a given url? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743987825a2571592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论