Javascript 如何使用javascript下载网页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12609121/
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-08-23 10:49:30  来源:igfitidea点击:

How to download a webpage using javascript

javascript

提问by MMS

Possible Duplicate:
Can Javascript read the source of any web page?

可能的重复:
Javascript 可以读取任何网页的源代码吗?

I want to download a file using javascript and parse it. I need a function like php's file_get_contents(). I looked for it, but I couldn't find anything.

我想使用javascript下载一个文件并解析它。我需要一个像 php 的函数file_get_contents()。我寻找它,但我找不到任何东西。

Update: Thanks for solutions, but I forgot to write that AJAX doesn't suite this problem, because I don't have any access to the remote address and have cross-domain requesting problem. I can't setup any php proxy either, because my host blocks access to fopen. I would prefer a pure javascript solution.

更新:感谢您的解决方案,但我忘了写 AJAX 不适合这个问题,因为我无法访问远程地址并且有跨域请求问题。我也无法设置任何 php 代理,因为我的主机阻止了对 fopen 的访问。我更喜欢纯 javascript 解决方案。

PS: Sorry, but the question was really a duplicate (I didn't know that) and the solution provided hereperfectly suits this problem.

PS:对不起,但这个问题确实是重复的(我不知道),这里提供的解决方案非常适合这个问题。

回答by geekman

Look into the XMLHttp Request

查看 XMLHttp 请求

http://www.w3schools.com/xml/xml_http.asp

http://www.w3schools.com/xml/xml_http.asp

or the load, $.get, $.post and $.ajax methods of JQuery.Here is a sample

或者 JQuery 的 load、$.get、$.post 和 $.ajax 方法。这里是一个示例

var request = new XMLHttpRequest(); 
request.open("GET", 'http://www.url.com');
request.onreadystatechange = function() { 
if (request.readyState === 4 && request.status === 200) {

//response handling code

}
};
request.send(null); // Send the request now

回答by jonr

Short question, short answer: You probably want to play around with some Ajax. Either by calling a local php script making a file_get_contents()and returning it the the page, or directly calling an external URL. Your browser might not allow you to do this though.

简短的问题,简短的回答:您可能想尝试一些 Ajax。要么通过调用本地 php 脚本制作 afile_get_contents()并将其返回到页面,要么直接调用外部 URL。不过,您的浏览器可能不允许您这样做。

Added:You updated your question saying you prefer a pure javascript solution. I don't think you can since you're trying to fetch something that is not JSONP. Also, you say your host blocks fopen(), I used to be on a hosting where they did the same. I was surprised to find they did NOT block the use of sockets, here's my workaround:

补充:您更新了您的问题,说您更喜欢纯 javascript 解决方案。我不认为你可以,因为你试图获取不是JSONP 的东西。另外,你说你的主机块fopen(),我曾经在主机上做同样的事情。我很惊讶地发现他们没有阻止使用 sockets,这是我的解决方法:

$server = "www.example.com";
$path = "/path/index.html";
$type = "HTTP/1.1";

$fp = fsockopen($server, 80, $errno, $errstr, 30);
if (!$fp) echo "$errstr ($errno)<br />\n";
else {
    $out  = "GET $path $type\r\n";
    $out .= "Host: $server\r\n";
    $out .= "User-Agent: Mozilla 4.0\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    $contents = "";
    while (!feof($fp)) $contents .= fgets($fp, 128);
    fclose($fp);
    echo $contents;
}

I realize you didn't want a PHP proxy solution, I think you might have no other choice. Give it a try, works great for me. If your hosting also is blocking fsockopen()you might be out of luck.

我意识到您不想要 PHP 代理解决方案,我认为您可能别无选择。试试看,对我很有用。如果您的托管也被阻止,fsockopen()您可能会不走运。

回答by Gung Foo

Have a look at XMLHttpRequest. It asynchronously downloads a file from the web.

看看XMLHttpRequest。它从网络异步下载文件。

If you plan on using jquery, have a look at .ajax(), which wraps XMLHttpRequest.

如果您打算使用jquery,请查看包装 XMLHttpRequest 的 .ajax()。

回答by Rahul

Iframe:Place an invisible iframe on your page,

iframe:在您的页面上放置一个不可见的 iframe,

<iframe id="frame1" style="display:none"></iframe>

Trigger a download [click or some other eventt handler etc], and set the URL of the Iframe. For example , "/location/file"

触发下载[点击或其他一些事件处理程序等],并设置 Iframe 的 URL。例如 ,"/location/file"

var iframe =  document.getElementById("frame1");
iframe .src = "/location/file";

This will trigger download from the browser.

这将触发从浏览器下载。

Another approach will be to to simply navigate to the download url, browser figures out that MIME type cannot be displayed and will present a download dialog. Use

另一种方法是简单地导航到下载 url,浏览器发现 MIME 类型无法显示,并会显示一个下载对话框。用

window.location.href = "/location/file";