13
10

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 5 years have passed since last update.

url stringからFileオブジェクトを作る

Posted at

問題

<input type="file"/>で選択したファイルをプレビュー表示などするためにJavaScript-Load-Imageなどを使ったりしますが、反対の事をしたい場合どうするのか?

ファイル選択(Fileオブジェクト) -> blob -> URL

URL -> blob -> Fileオブジェクト

解決

fetchを使う。

fetch(urlString)
  .then(response => response.blob())
  .then(blob => new File([blob], "image.jpg"))
  .then(file => {
    // fileはFileオブジェクト
  })

File周りの良い記事
https://stackoverflow.com/questions/44070437/how-to-get-a-file-or-blob-from-an-url-in-javascript
https://qiita.com/kawa64372358/items/f8f729a29f0b980f7e32
https://qiita.com/TypoScript/items/0d5b08cecf959b8b822c

13
10
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
13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?