Moxie library
I found a library capable of doing what you’re asking.
It is a polyfill listed on modernizr’s Github called Moxie.
The library is fantastic, except to start development. I had to compile it, and then I started testing... I fixed some compilation errors, and some errors in the generated Javascript, then finally it worked.
I used the following parts of the Moxie:
- Fileinput: component that allows you to select a file and use the files that are selected using a
FileReader
- Filereader: allows you to read a file
- Image: allows you to manipulate images, such as resize them
Due to the difficulty I had in compiling, I decided to create a repository on Github with the already compiled version:
Example of use
I put it in the folder \v1.2.1\samples
, the example of how to do what you said:
image-preview-before-upload.htm
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="moxie.min.js"></script>
<script>mOxie.Env.swf_url = 'Moxie.swf';</script>
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new mOxie.FileReader();
reader.onload = function (e) {
var img = new mOxie.Image();
img.onload = function() {
img.downsize({width:100});
var tgt = document.getElementById("target");
img.embed(tgt);
};
img.load(e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}
</script>
</head>
<body>
<form id="form1">
<div id="container">
<a id="file-picker" href="javascript:;">Browse...</a>
</div>
<script>
var fileInput = new mOxie.FileInput({
browse_button: 'file-picker', // or document.getElementById('file-picker')
container: 'container',
accept: [
{title: "Image files", extensions: "jpg,gif,png"} // accept only images
],
multiple: true // allow multiple file selection
});
fileInput.onchange = function(e) {
// do something to files array
//console.info(e.target.files); // or this.files or fileInput.files
readURL(this);
};
fileInput.init(); // initialize
</script>
<div id="target"></div>
</form>
</body></html>
Compatibility
Works in the IE8 and IE9. Also in the Chrome, Firefox and Opera.
I had trouble with the Safari, IE6 and IE7.
Perhaps it’s best to use a selective approach between my response, your own code (should work in Safari), and Reply by @Carlosmartins (which uses Alphaimageloader, so I searched it works on IE6 and IE7). You could use conditional comments to achieve full compatibility if you want.
Dependencies
To work on IE8 and IE9, Moxie requires or Flash or Silverlight. It seems that they have plans to support the use of Java as well, but for now they don’t have.
Hello, which method or object does not work on IE8 and 9 ?
– PauloHDSousa
@Paulohdsousa Editei!
– Silvio Andorinha
@You’ve already tested this: http://stackoverflow.com/a/11277737/2256325 ?
– Sergio
@Sergio, had not seen.. but still unfortunately does not solve 100% of the problem because in IE 9 still does not work
– Silvio Andorinha
I guess the ones before the 10 there is no way anyway.
– bfavaretto
What is this method for?
– PauloHDSousa
Because @bfavaretto said that’s not going to work. What do you want to do? preview images? Something like this: http://stackoverflow.com/q/4459379/2256325 ?
– Sergio
face I don’t even know what to use for it to work. IE is a life lag. It serves absolutely nothing. In fact it serves only to give problems
– Gabriel Schmidt Cordeiro
I respect your opinion, but there really are situations where the customer requires the system to work (sometimes even exclusively) in the IE, including multinational companies.
– WyllianNeo
@Silvioandorinha you just want to make a "preview" of the image?
– Guilherme Nascimento
I didn’t actually work with IE 10, so I don’t know that method. I imagine you will have to do a function to verify which version of the customer’s IE and if it is version before the IE 10, work differently. Depending on your objective, there is a way that applies to previous and current versions.
– WyllianNeo
Just as a complement I am on a project for a large company that to homologue it needs to run on IE8. So, unfortunately, personal taste does not serve as a parameter or guide.
– Jothaz
I found this in the English Stack Overflow. http://stackoverflow.com/questions/14206127/createobjecturl-does-not-work-in-ie10 Look if it can help you.
– Marconi
This issue is resolved?
– Miguel Angelo
@Miguelangelo.
– Silvio Andorinha
Is it possible to install something on the computer? Like a server... or Flash in the browser?
– Miguel Angelo