Skip to content Skip to sidebar Skip to footer

How To Download A Gif From A Given Url Using Javascript

I am trying to download a GIF from Giphy (just need to download it, I don't need to display it on the browser). I tried using the solution in this question this question however i

Solution 1:

This works for me, I took some of the code from here https://randomtutes.com/2019/08/02/download-blob-as-file-in-javascript/

(async () => {
  //create new a elementlet a = document.createElement('a');
  // get image as bloblet response = awaitfetch('https://media2.giphy.com/media/DvyLQztQwmyAM/giphy.gif?cid=e9ff928175irq2ybzjyiuicjuxk21vv4jyyn0ut5o0d7co50&rid=giphy.gif');
  let file = await response.blob();
  // use download attribute https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes
  a.download = 'myGif';
  a.href = window.URL.createObjectURL(file);
  //store download url in javascript https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes#JavaScript_access
  a.dataset.downloadurl = ['application/octet-stream', a.download, a.href].join(':');
  //click on element to start download
  a.click();
})();

Solution 2:

I managed to get this working in Chrome and Firefox too by appending a link to the to document.

var link = document.createElement('a');
link.href = 'images.jpg';
link.download = 'Download.jpg';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);

Post a Comment for "How To Download A Gif From A Given Url Using Javascript"