天天看點

html5 顯示tif,javascript - Loading tiff files into HTML5 canvas - Stack Overflow

I am trying to load .tif files onto an HTML5 canvas. After some research, I discovered that I might be able to do it using FileReader. Most of my code below is from http://www.nczonline.net/blog/2012/05/15/working-with-files-in-javascript-part-2/ . Unfortunately, the following test page fails on .tif files (it works on other image files) saying 'Image could not load' in Firefox and Chrome (let's ignore IE for this test). Am I missing something?

function loadFile(e) {

var file = e.target.files[0];

var reader = new FileReader();

reader.onload = function(event) {

var canvas = document.getElementById('myCanvas');

var context = canvas.getContext('2d');

var dataUri = event.target.result,

img = new Image();

// wait until the image has been fully processed

img.onload = function() {

context.drawImage(img, 0, 0);

};

img.onerror = function () {

console.error('Image could not load.');

}

img.src = dataUri;

};

reader.onerror = function(event) {

console.error("File could not be read. Code " + event.target.error.code);

};

reader.readAsDataURL(file);

}