1

What I need is image upload within a form:

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

I have read a lot, including:

  1. Image resizing client-side with JavaScript before upload to the server
  2. Show an image preview before upload
  3. https://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/

Then come up with a solution, but it does not work:

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



<script>

function handleFileSelect(evt) {
    var files = evt.target.files;

    // 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) {

var img = document.createElement('img');
img.src = e.target.result;
var canvas = document.createElement('canvas');

var MAX_WIDTH = 800;
var MAX_HEIGHT = 600;
var width = img.width;
var height = img.height;

if (width > height) {
  if (width > MAX_WIDTH) {
    height *= MAX_WIDTH / width;
    width = MAX_WIDTH;
  }
} else {
  if (height > MAX_HEIGHT) {
    width *= MAX_HEIGHT / height;
    height = MAX_HEIGHT;
  }
}
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);

var dataurl = canvas.toDataURL("image/png");

         // Render thumbnail.
          var span = document.createElement('span');
          span.innerHTML = 
          [
            '<img class="thumb" src="', 
            dataurl,
            '" 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>

It won't do resize and won't do preview. However, multiple full size image upload still works.

EDIT: It does resize and does preview. But after I click submit the form, the full size original images are still uploaded.

Am I doing something wrong?

valpa
  • 307
  • 1
  • 3
  • 11
  • For previews, you'd be better off getting the base64 value of the image and feeding it into the `src` of a pre-existing `img` tag instead of having to work with canvas. – Supreme Dolphin Aug 10 '17 at 13:18
  • I need resize first. Without resizing I can replace dataurl with e.target.result then preview will work. But this is not what I need. – valpa Aug 10 '17 at 13:43

1 Answers1

0

Full size original images are submitted because you're not changing the images themselves, rather creating new images. But the <input[file]>still points to the original images. You can ignore these <input[file]> and instead submit the resized image's dataUrl within <form/> possibly through a hidden <input> field.

Simply use the resized images, convert them to base64 dataUrls, write to some hidden inputs within the form to submit.

var dataurl = canvas.toDataURL("image/png");

document.getElementById('hiddenInputForImage').value=dataurl;
coding_idiot
  • 12,860
  • 9
  • 56
  • 111
  • Thanks. I tried. add your code, add input in form . But the upload still not success. No filename and No image content uploaded to server. – valpa Aug 11 '17 at 00:24
  • nope, that's not what I intended. The hidden fields are just `` which will contain the dataURL which we want to upload. You can create images out of dataUrl on the server-side if required or just serve them as it is. – coding_idiot Aug 11 '17 at 18:44
  • @valpa see https://stackoverflow.com/questions/40067544/how-to-upload-image-from-img-tag – coding_idiot Aug 11 '17 at 18:46