5

I am using the HTML5 File API & FileReader.

HTML:

<div id="holder"></div> 

JS:

<script>
var holder = document.getElementById('holder'),
    state = document.getElementById('status');

if (typeof window.FileReader === 'undefined') {
  state.className = 'fail';
} else {
  state.className = 'success';
  state.innerHTML = 'File API & FileReader available';
}

holder.ondragover = function () { this.className = 'hover'; return false; };
holder.ondragend = function () { this.className = ''; return false; };
holder.ondrop = function (e) {
  this.className = '';
  e.preventDefault();

  var file = e.dataTransfer.files[0],
      reader = new FileReader();
  reader.onload = function (event) {
    console.log(event.target);
    holder.style.background = 'url(' + event.target.result + ') no-repeat center';
  };
  console.log(file);
  reader.readAsDataURL(file);

  return false;
};
</script>

How can I retrieve EXIF meta data from the uploaded image?

I tried to use this.

HTML:

<img src="image1.jpg" id="img1" exif="true" />

JS:

console.log($("#img1").exifPretty());

This only returns an empty set.

I also use the FileReader JQuery Plugin.

When I use the load function I get a file which is an extension of the original File object.

on:
    load: function(e, file) { }

But how do I retrieve the EXIF meta data from it?

3
  • 1
    stackoverflow.com/questions/10341685/… Commented Sep 24, 2013 at 12:27
  • @Andreas I use the FileReader JQuery Plugin to retrieve the file: github.com/bgrins/filereader.js I have a file object but I cannot manage to get the meta data. See my question update. Can you please post an answer? Commented Sep 24, 2013 at 14:14
  • 2
    In your example you're not using the FileReader plugin so did you even try the solution of the linked SO question? Use .readAsBinaryString from a FileReader, wrap this string into a BinaryFile (included in EXIF lib) feed this object to EXIF.readFromBinaryFile(binaryFileObject) Commented Sep 24, 2013 at 15:54

3 Answers 3

2

If you're getting EXIF undefined, then use var EXIF = require('./exif.js'); FTW.

I managed to get that beast working without magic tricks (quick&dirty trial&error result):

'use strict';
var EXIF = require('./exif.js');

$(function() {
    $('#fileinput').on('change', function(){
       var files = this.files,
           i=0;
        for(i=0; i<files.length;++i){
            previewImage(this.files[i]);
        }
    });
    function previewImage(file) {
        var gallery = $('#gallery'),
            thumb = null,
            img = null,
            reader= null;
        if(!file.type.match('image/*')){
            throw 'File type must be an image';
        }

        thumb = $('<div />',{class: 'thumbnail'}).appendTo(gallery);
        img = $('<img />');
        reader = new FileReader();
        reader.onload = function(e){
            img.prop('src',reader.result);
            // important for exif-js! Set src attribute after calling img.prop
            img.src = img.prop('src');
            img.appendTo(thumb);
            EXIF.getData(img, function() {
                console.log(EXIF.pretty(img));
            });
        };
        reader.readAsDataURL(file);
    }

});
Sign up to request clarification or add additional context in comments.

Comments

0

with help of exif.js , the following script can get the exif from file input

$('#imageupload').change(function(){
    var file = this.files[0];
    var reader = new FileReader();
    reader.onload = function(e) {

        var exif = EXIF.readFromBinaryFile(this.result);
        console.log(exif);


     }

     reader.readAsArrayBuffer(file);


});

Comments

-2

This is the solution:

on:
    load: function(event, file) {
    // get image meta data
    var base64 = event.target.result.replace(/^.*?,/,'');
    var binary = atob(base64);
    var exif = EXIF.readFromBinaryFile(new BinaryFile(binary));
}

2 Comments

I dont know why its says "ReferenceError: exif is not defined", even i have added jquery.exif.js.
I think this answer requires the exif-js dependency, hence the EXIF not defined errors you are receiving: github.com/exif-js/exif-js (That said it's not a great answer, check this one instead: stackoverflow.com/questions/10341685/…)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.