首页 > 代码库 > js上传并且预览图片

js上传并且预览图片

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript" src = "jquery.js"></script>
</head>
<body>

<input id="upload" type="file" name="files" />

<div id = "box">
    
</div>
<script type="text/javascript">
var upload = document.getElementById(upload);
upload.addEventListener(change, function() {
      var file = upload.files[0];
      console.log(file.size);


      var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function(e) {
      // var img = new Image();
      // img.src = http://www.mamicode.com/this.result;
      // console.log(this.result);
      src = this.result;
      $(#box).append(
              "<img src =http://www.mamicode.com/"+src+ ">"
          )

    };


}, false);

</script>

</body>
</html>

http://www.open-open.com/lib/view/open1456113131948.html

js上传并且预览图片