Display image from desktop in a page

这一生的挚爱 提交于 2019-12-13 09:30:05

问题


I dynamicaly generate a php page with the information I added in a previous form. I want to add (not upload) an image in my form from my desktop and then display it in my generated page. I'm not asking you to do it but I just want to know if it's possible or not.

Thank you


回答1:


Yes

(but not permanently)

That's the answer. More specific what you have to do:

Let the user select the file via a form input="file", read the file via JavaScript and output the blob on the site.

Here's a good tutorial: http://www.html5rocks.com/en/tutorials/file/dndfiles/

The main code they are using:

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);


来源:https://stackoverflow.com/questions/25487454/display-image-from-desktop-in-a-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!