admin管理员组

文章数量:1400201

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?

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?

Share Improve this question edited Sep 24, 2013 at 14:12 Michael asked Sep 24, 2013 at 12:24 MichaelMichael 33.3k50 gold badges223 silver badges374 bronze badges 3
  • 1 stackoverflow./questions/10341685/… – Andreas Commented Sep 24, 2013 at 12:27
  • @Andreas I use the FileReader JQuery Plugin to retrieve the file: github./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? – Michael 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) – Andreas Commented Sep 24, 2013 at 15:54
Add a ment  | 

3 Answers 3

Reset to default 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);
    }

});

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


});

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

本文标签: javascriptRetrieve EXIF Image Meta Data from HTML5 FileApi loaded ImageStack Overflow