javascript: get base64 of input field (type=file) in IE9

喜你入骨 提交于 2019-12-13 14:07:28

问题


I need to upload an image, something like this

<form>
    <input type="file">
</form>

However, I want to crop/resize the file before uploading. The cropping and resize is no problem, but how do I get the base64 from the input file element? In IE10 and the other browsers I can it like this:

if (this.files.length === 0) {
    return;
}

var file = this.files[0];

if (file.type.match(/image.*/)) {
    var reader = new FileReader();
    reader.onload = function (event) {
        cropAndResize(event.target.result);
    };
    reader.readAsDataURL(file);
}

However, in IE9 this.files is undefined. How can I access the base64 of the uploaded image (without a round trip to the backend of course!) in IE9 ? Here is a jsfiddle


回答1:


IE9 does not support the FileReader API and therefore cannot do what you want using pure JavaScript. If you want IE9 support, then you'd need to use something like this Flash solution in order to achieve the same result.



来源:https://stackoverflow.com/questions/20598654/javascript-get-base64-of-input-field-type-file-in-ie9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!