最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Download or just save an image file from a website using js - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Jul 29, 2009 at 11:29 scunliffe 63.6k26 gold badges131 silver badges165 bronze badges asked Jul 29, 2009 at 11:24 praveenjayapalpraveenjayapal 38.5k31 gold badges74 silver badges74 bronze badges 3
  • Or else is it possible to download all the image from a given url - along with the original size – praveenjayapal Commented Jul 29, 2009 at 11:30
  • 1 It is not clear, why you want to do this in JS? Will this be a part of some site of yours? If you need a standalone script/application for that, JS is not the right tool for the job. – Maxim Sloyko Commented Jul 29, 2009 at 11:30
  • ok please tell me which one will be suitable for this job – praveenjayapal Commented Jul 29, 2009 at 11:32
Add a comment  | 

7 Answers 7

Reset to default 7

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

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.

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.

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

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

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.

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

发布评论

评论列表(0)

  1. 暂无评论