JavaScript combines filereader to realize upload picture _javascript skill

Source: Internet
Author: User

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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.