javascript 中的 wget 从给定的 url 下载文件的等价物是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8236735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:45:04  来源:igfitidea点击:

What is the equivalent of wget in javascript to download a file from a given url?

javascriptdownloadwget

提问by Sangeeth Saravanaraj

"wget http://www.example.com/file.doc" downloads that file to the local disk.

“wget http://www.example.com/file.doc”将该文件下载到本地磁盘。

What is the equivalent of the above in javascript? for example, consider the following html snippet.

在 javascript 中与上面的等价物是什么?例如,考虑以下 html 片段。

<html>
<head>
   <script language="JavaScript">
      function download_file() {
         var url = "http://www.example.com/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 compliant with all the browsers.

请提出与所有浏览器兼容的解决方案。

Sangeeth.

桑吉斯。

采纳答案by Sangeeth Saravanaraj

After 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);

托管文件的网站不允许我们使用window.location = url;window.open(url);

Finally we had to use the data-downloadurlsupport from HTML5as follows

最后,我们不得不使用data-downloadurl从支持HTML5如下

<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.

我们将此 html 嵌入到主机 html 中,当点击链接时,它会触发下载。

回答by hafichuk

Why not use:

为什么不使用:

 function download_file() {
   var url = "http://www.example.com/file.doc"
   window.location = url;
 }

See https://developer.mozilla.org/en/DOM/window.location

请参阅https://developer.mozilla.org/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.com/file.doc"
   window.open(url);
 }

See https://developer.mozilla.org/en/DOM/window.open

https://developer.mozilla.org/en/DOM/window.open

回答by Wazy

First thing that always comes in mind of every answerer to this question is executing wget shell command from java script.I'm almost certain that that's not possible because of major security risk.

这个问题的每个回答者总是想到的第一件事是从 java 脚本执行 wget shell 命令。我几乎可以肯定这是不可能的,因为存在重大安全风险。

You pretty much need to have ajax which sends command to command line either through php, or another scripting language via ajax...

您几乎需要有 ajax,它通过 php 或通过 ajax 的另一种脚本语言将命令发送到命令行...

You could probably make that happen with something like http://www.phantomjs.org/
I am saying probably because I read it from somewhere.

你可能可以通过像http://www.phantomjs.org/这样的东西来实现这一点,
我这么说可能是因为我从某个地方读到了它。