-1

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 Answer 1

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);
23
  • @wumm Thank you so much ! That's a good way to start for me ! May Have I another request for you... I want to read it in the generated page. Can I do that ? I mean, when I check the source, it's data: (nothing else)
    – Mathieu Fe
    Aug 25, 2014 at 14:02
  • You mean the created img looks like data:....? Yes, that's the only way to display it. If you want to get the path to the image that's not possible because of security restrictions.
    – idmean
    Aug 25, 2014 at 14:04
  • Oh ok I see. So, if I want to generate a page with 3 images from my desktop and then Download the HTML page and the 3 images, I can't ?
    – Mathieu Fe
    Aug 25, 2014 at 14:07
  • Only with data blobs, not with the path to your desktop, no.
    – idmean
    Aug 25, 2014 at 14:11
  • 1
    @DanFromGermany It's indeed possible to store images in localStorage: codepen.io/idmean/pen/nKwcx (I just had to test it)
    – idmean
    Aug 25, 2014 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.