8

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

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.

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.

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

Community
  • 1
  • 1
MMS
  • 408
  • 2
  • 5
  • 9
  • 3
    Do you want to download a file or a webpage? A webpage is a set of multiple files. Also what context do you want the code in. A browser plugin, a webpage script or a server side javascript? – Caimen Sep 26 '12 at 19:38
  • 1
    What happened to the good old XMLHttpRequest?? – geekman Sep 26 '12 at 19:38
  • @Caimen: I want to download an HTML file. By that means, it's only a file, because I just need to parse it and I don't need the styling, etc. – MMS Sep 26 '12 at 19:48

4 Answers4

8

Look into the XMLHttp Request

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

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

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
geekman
  • 2,224
  • 13
  • 17
4

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.

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:

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

jonr
  • 136
  • 2
  • 10
2

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

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

Gung Foo
  • 13,392
  • 5
  • 31
  • 39
1

Iframe: Place an invisible iframe on your page,

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

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

window.location.href = "/location/file";
Rahul
  • 1,549
  • 3
  • 17
  • 35