94
97

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.

HTML5 File API で画像をアップロードしてプレビュー

Last updated at Posted at 2014-07-10
  1. <input type='file'/> で画像を選択させる
  2. input.onchange が発火するのでコールバックをとる
  3. File APIがあれば input.files[0] にFileオブジェクトが入ってるの抜き取る
  4. FileReader の FileReader#readAsDataURL に Fileオブジェクトを渡す
  5. FileRead#onload が発火するのでevent.target.result に Data uri が入ってる
  6. imgのsrcにData uri ぶち込む
<img class='image-preview'/>
<input type='file' class='file-selector'>upload</input>
$preview = $('img.image-preview')
$file = $('input.file-selector')
$file.on 'change', =>
  [file] = $file.get(0).files
  fr = new FileReader
  fr.readAsDataURL(file)
  fr.onload = (event) =>
    $preview.attr src: event.target.result
94
97
1

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
94
97

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?