jquery upload base64 bit image

Source: Internet
Author: User


<input type= "file" value= "Upload" id= "articleimg


1$('#articleImgBtn'). Change (function () {2Run This, function (data) {3 uploadimage (data);4 }); 5 });6 7 function Run (input_file, get_data) {8 /*input_file: File button Object*/ 9 /*Get_data: Method to execute after successful conversion*/ Ten if(typeof(filereader) = = ='undefined') { OneAlert"Sorry, your browser does not support FileReader, can not convert the picture to Base64, please use modern browser operation! "); A}Else { - Try { - /*image to Base64 core code*/ the varFile = input_file.files[0]; - //Here we judge the type if it is not a picture to return to remove any file can be uploaded - if(!/image\/\w+/. Test (File.type)) { -Alert"Make sure the file is an image type"); + return false; - } + varReader =NewFileReader (); AReader.onload =function () { atGet_data ( This. Result); - } - reader.readasdataurl (file); -}Catch(e) { -Alert'picture turn Base64 Error! '+e.tostring ()) - } in } - } to + function Uploadimage (img) { - //determine if you have the option to upload files the varImgpath = $ ("#articleImgBtn"). Val (); * if(Imgpath = ="") { $Alert"Please select upload image! ");Panax Notoginseng return; - } the //determine the suffix name of the uploaded file + varstrextension = Imgpath.substr (Imgpath.lastindexof ('.') +1); A if(Strextension! ='jpg'&& strextension! ='gif' the&& strextension! ='PNG'&& strextension! ='BMP') { +Alert"Please select a picture file"); - return; $ } $ $.ajax ({ -Type"POST", -URL: ' Upload image interface ', theData: {Token:token,file:img.substr (Img.indexof (',') +1)},//base64 the preceding string data:image/png;base64 as appropriate, delete -Cachefalse,Wuyi success:function (data) { theAlert"Upload Successful"); -$("#articleImg"). attr ('src',json.parse (data). ImageUrl); Wu }, - error:function (XMLHttpRequest, Textstatus, Errorthrown) { AboutAlert"upload failed, please check network and try again"); $ } - }); -}

jquery upload base64 bit image

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.