<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