15
16

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.

ファイルアップロードでお手軽な画像プレビュー

Last updated at Posted at 2015-01-27

登録フォームでアップロードする画像のプレビューをお手軽に表示する方法。
以前はajaxを使って裏でサーバにアップロードしてimgのsrcを書き換えるなんてことをやってたけど、この方法だとクライアント側だけで解決する。
IE10以降/Chrome/FFで動作確認できた(safariはmac持っていないので未確認)

html

<img id="preview" src="/images/blank.png" />
<input type="file" id="picture" name="picture" />

js

$('#picture').change(function() {
  try {
    var reader = new FileReader();
    reader.onload = function(e) {
      $('#preview').attr('src', e.target.result);
    }
    reader.readAsDataURL(this.files[0]);
  } catch(e) {
  }
});
15
16
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
15
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?