praveenjayapal
praveenjayapal

Reputation: 38609

Download or just save an image file from a website using js

I want to download or just need to save the image file from a website using the javascript in pure coding itself.. Because i want to download nearly 1000 jpeg, so i would like to write a function to call and download those images.

URL is available, from that i want to download the specific image

Please guide regarding this..

Upvotes: 5

Views: 20256

Answers (7)

Bharti Ladumor
Bharti Ladumor

Reputation: 1704

fileUrl = "http://www.b2hp.com/myProfilePic.jpg";

var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function () {
  var a = document.createElement('a'); // create html element anchor
  a.href = window.URL.createObjectURL(xhr.response); // xhr.response is a blob
  a.download = "TEMP-1.jpg"; // Set the file name.
  a.style.display = 'none'; // set anchor as hidden
  document.body.appendChild(a);
  a.click();
  a.remove()
};
xhr.open('GET', fileUrl);
xhr.send();

Upvotes: 7

nilamo
nilamo

Reputation: 1942

A scripting language could handle this pretty easily. Python in particular, with BeautifulSoup, would be fairly easy to work with in this case.

If you're looking, any language that can download pages and has a library available to parse HTML would work just fine, BS just happens to be the only one I have extensive knowledge of (and can therefore recommend).

Upvotes: 0

Amir
Amir

Reputation: 1259

if you want to load images by code you should read this article about image preloading: http://www.webreference.com/programming/javascript/gr/column3/

it helped me to build a class to fetch images from URLs and get notifications when its done.

Upvotes: 0

TFM
TFM

Reputation: 544

Like David says, JS won't do it.

You either need a standalone download manager, or a browser plug-in variant. Most download managers are pretty good at downloading a sequence of files, for example files starting with "img0001.jpg", and ending with "img9999.jpg".

Upvotes: 1

Stuart K
Stuart K

Reputation: 3292

There's the Firefox extension FlashGot which can download all images/audio etc. on a page in one go.

Upvotes: 0

scunliffe
scunliffe

Reputation: 63676

Depending on your needs, you may want to look into DownThemAll (a Firefox addon)

It will let you bulk download linked images, movies etc. from a web site.

Note it is a user tool, not a developer tool, thus you can't do the whole thing programatically.

alt text

Upvotes: 3

Quentin
Quentin

Reputation: 944522

JavaScript, in the standard browser security context, is not allowed to write to the user's file system (beyond any side effects of caching).

You might be able to achieve what you want using a browser plug in / add on / extension / etc — but even the broad strokes of how to achieve that would depend on the browser you are targetting.

Frankly, this sounds more like a job for a spider such as wget than it does for JavaScript.

Upvotes: 3

Related Questions