html5图片上传

发布于 2024-12-27 04:20:34 字数 231 浏览 1 评论 0原文

我正在尝试用 html5 制作一个非常简单的图像上传器。

<input type="file" multiple=""/>

我想做的就是显示上传的内容,而不使用 PhP 或任何东西。我可以使用与此类似的代码吗?

<img src="WHATEVER WAS UPLOADED"/>

谢谢!

I'm trying to make a VERY simple image uploader in html5.

<input type="file" multiple=""/>

All I would like to do is display what is uploaded without using PhP or anything. Could I use code similar to this?

<img src="WHATEVER WAS UPLOADED"/>

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪是无色的血 2025-01-03 04:20:34

我发现

http://www.html5rocks.com/en/tutorials/file/dndfiles/

很有帮助。

如果您不想将图像推送到某个服务器(我从您的问题中假设这一点),您可以在本地更新图像:

<style>
  .thumb {
    height: 75px;
    border: 1px solid #000;
    margin: 10px 5px 0 0;
    }
 </style>

<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>

<script>
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

   // Loop through the FileList and render image files as thumbnails.
   for (var i = 0, f; f = files[i]; i++) {

     // Only process image files.
     if (!f.type.match('image.*')) {
       continue;
     }

     var reader = new FileReader();

     // Closure to capture the file information.
     reader.onload = (function(theFile) {
       return function(e) {
          // Render thumbnail.
          var span = document.createElement('span');
          span.innerHTML = ['<img class="thumb" src="', e.target.result,
                        '" title="', escape(theFile.name), '"/>'].join('');
          document.getElementById('list').insertBefore(span, null);
    };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);

 </script>

或类似的东西。

i found

http://www.html5rocks.com/en/tutorials/file/dndfiles/

rather helpful.

if you dont want to push the image to some server ( i assume this from your question ), you can just update the image locally :

<style>
  .thumb {
    height: 75px;
    border: 1px solid #000;
    margin: 10px 5px 0 0;
    }
 </style>

<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>

<script>
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

   // Loop through the FileList and render image files as thumbnails.
   for (var i = 0, f; f = files[i]; i++) {

     // Only process image files.
     if (!f.type.match('image.*')) {
       continue;
     }

     var reader = new FileReader();

     // Closure to capture the file information.
     reader.onload = (function(theFile) {
       return function(e) {
          // Render thumbnail.
          var span = document.createElement('span');
          span.innerHTML = ['<img class="thumb" src="', e.target.result,
                        '" title="', escape(theFile.name), '"/>'].join('');
          document.getElementById('list').insertBefore(span, null);
    };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);

 </script>

or somesuch.

行雁书 2025-01-03 04:20:34

您可以使用 HTML5 File Api 对此进行存档。

以下教程应该可以帮助您入门:在 JavaScript 中读取本地文件

You could archive this with HTML5 File Api.

Following tutorial should get you started: Reading local files in JavaScript

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文