<Scriptsrc= "Jquery-1.7.2.min.js"></Script><Body> <inputtype= "File"ID= "img"name= "img" /> <imgID= "Test"src="" /> <inputtype= "button"ID= "Submit"value= "OK"onclick= "Submit ()" /> <Script> functionSubmit () {varF=document.getElementById ("img"). files[0]; varReader= NewFileReader (); //Closure to capture the file information.Reader.onload= (function(thefile) {return function(e) {varI=document.getElementById ("Test"); I.SRC=Event.target.result; varData=Compress (i, -); varFD= NewFormData (); Fd.append ("img", data); $.ajax ({URL:"Webform1.aspx?type=upload", type:"POST", DATA:FD, ProcessData:false, //tell jquery not to process the data sentContentType:false, //tell jquery not to set the Content-type request headerSuccess:function(data) {$ (". Loadingbox"). Hide (); if(Data== 0) Myalert ("Tips", "Save failed"); ElseMyalerttoone ("Tips", "saved successfully", "share.html"); } }); }; }) (f); Reader.readasdataurl (f); } functionCompress (source_img_obj, quality, Output_format) {varMime_type= "Image/jpeg"; if(Output_format!=undefined&&Output_format=="PNG") {Mime_type= "Image/png"; } varCVS=Document.createelement ('Canvas'); //Naturalwidth The width of the real pictureCvs.width=Source_img_obj.naturalwidth; Cvs.height=Source_img_obj.naturalheight; varCTX=Cvs.getcontext ("2d"). DrawImage (Source_img_obj,0, 0); returnCvs.todataurl (mime_type, quality/); } </Script></Body>Front-end code
protected voidPage_Load (Objectsender, EventArgs e) {Base64stringtoimage (request.form["img"]); } Private voidBase64stringtoimage (stringinputstr) { if(string. Isnullorwhitespace (INPUTSTR))return; Try { stringFilePath ="e:\\bb.jpg"; byte[] arr = convert.frombase64string (inputstr.substring (Inputstr.indexof ("Base64,") +7). Trim (' /')); using(MemoryStream ms =NewMemoryStream (arr)) {Bitmap BMP=NewBitmap (MS); //Create a new BMP2 variable for the second bitmap type. Bitmap BMP2 =NewBitmap (BMP, BMP. Width, BMP. Height); //copy the first BMP into the BMP2Graphics draw =graphics.fromimage (BMP2); Draw. DrawImage (BMP,0,0); Draw. Dispose (); Bmp2. Save (FilePath, System.Drawing.Imaging.ImageFormat.Jpeg); Ms. Close (); } } Catch(Exception ex) {}}Back-end code
(c#.net) front-end JS image compression will get the Base64 string saved as a picture