About the file API here is not explained in detail, small partners to their own Niang it, we will use the file handle to read the file content, this is achieved through the FileReader, through the FileReader interface, we can asynchronously load the contents of the file into memory, give a JS variable.
Copy Code code as follows:
function getimgsrc (target, callback) {
if (window. FileReader) {
var opreviewimg = null, Ofreader = new window. FileReader ();
Ofreader.onload = function (ofrevent) {
opreviewimg = new Image ();
var type = Target.files[0].type.split ("/") [1];
var src = oFREvent.target.result;
OPREVIEWIMG.SRC = src;
if (typeof callback = = "function") {
Callback (opreviewimg, target, type, SRC);
}
return OPREVIEWIMG.SRC;
};
Return (function () {
var afiles = target.files;
if (Afiles.length = = 0) {
Return
}
if (! Isimgtype (Afiles[0].type)) {
Alert ("You must select a valid Image file!");
Return
}
if (Afiles[0].size > 1024 * 1024) {
Target.value = "";
Alert (' Please upload image file size less than 1M. ');
Return
}
Ofreader.readasdataurl (Afiles[0]);
})();
}
if (navigator.appname = = "Microsoft Internet Explorer") {
Return (function () {
document.getElementById ("Imagepreview"). Filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). src = Target.value;
})();
}
}
The above is JavaScript combined with FileReader implementation of the key code upload pictures, small partners like it?