I am trying to get the exif metadata of an image that is uploaded with an input tag in react, and have the following function attached to the input tag's property "onChange":
onChange(e) {
const { input: { onChange } } = this.props;
let img_arr = [];
for (let i = 0; i < e.target.files.length; i++) {
const file = e.target.files[i];
console.log('exif data');
console.log(EXIF.getData(file, () => {
var orientation = EXIF.getTag(this, "Orientation");
console.log('orientation');
console.log(orientation);
}));
img_arr.push({file: file, url: URL.createObjectURL(file)});
}
//console.log("printing img arr");
//console.log(img_arr);
onChange(img_arr);
}
However, I get EXIF is not defined. On the exif-js page, it is suggested to use window.onload. https://github.com/exif-js/exif-js. How does one use window.onload in a react component?
-- edit --
I changed my code to this, but still get undefined for orientation:
onChange(e) {
const { input: { onChange } } = this.props;
let img_arr = [];
for (let i = 0; i < e.target.files.length; i++) {
const file = e.target.files[i];
console.log('exif data');
EXIF.getData(file, function() {
const url = URL.createObjectURL(file);
const image = new Image();
image.onload = function() {
URL.revokeObjectURL(url);
const orientation = EXIF.getTag(this, 'Orientation');
console.log(orientation);
};
image.src = url;
});
img_arr.push({file: file, url: URL.createObjectURL(file)});
}
onChange(img_arr);
}