3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

REDIMPULZAdvent Calendar 2020

Day 8

jsで外部サイトのファイルをダウンロードする

Posted at

ファイルをダウンロードさせたい

aタグのdownload属性を指定するとページ移動ではなくファイル保存させることができますが
同一originでしか動作しません。

代替手段

fetchでファイルを取得後に「FileSaver」のsaveAsで保存させることで実現することができます
https://github.com/eligrey/FileSaver.js/

const videoDownload = async (url: string) => {
  const data = await fetch(url);
  const blob = await data.blob();
  saveAs(blob);
};
<a onClick="videoDownload('http://[ダウンロードさせたいファイルのURL]')"></a>
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?