javascript - Convert base64 to file type in React cropper js? -
i have created crop functionality using react cropper js. need send image file type. i'm able base64 string cropped image. when submit cropped image need send file type. here have included code crop function. how send cropped area image file?
here code,
var react = require('react'); var cropper = require('react-cropper').default; var myprofileedit = react.createclass({ getinitialstate:function(){ return { open:false, src:"", cropresult: null } }, onchange:function(e){ var self = this; $("#photo").val(''); function readurl(input) { if (input.files && input.files[0]) { var reader = new filereader(); reader.onload = function (e) { self.setstate({ open:true, src: reader.result }); } reader.readasdataurl(input.files[0]); } } $("#photo").one('change', function (e) { e.preventdefault(); var self = this; readurl(self); }); }, cropimage:function(){ if (typeof this.refs.cropper.getcroppedcanvas() === 'undefined') { return; } this.setstate({ open:false, cropresult: this.refs.cropper.getcroppedcanvas().todataurl() }); }, cancelcrop: function() { this.setstate({ open:false, src:"", cropresult:null }); }, crop:function(){ if (typeof this.refs.cropper.getcroppedcanvas() === 'undefined') { return; } this.setstate({ cropresult: this.refs.cropper.getcroppedcanvas().todataurl() }); }, render:function(){ return ( <div classname="view-container"> <div classname="scroll-content has-header"> <form classname="edit-profile"> <div classname="container"> <div classname="profile-photo"> <div classname="profile-photo-upload"> <div classname="img-preview" style={{ width: '100%', height: 120 }}> <img src="img/photo.png" width="120" alt="photo" /> </div> <span classname="upload-icon"> <input type="file" id="photo" onclick={this.onchange} /> <input type="text" name="ptyphoto" /> </span> </div> </div> </div> </form> </div> <div classname={"profile-popup "+(this.state.open ? "active" : "inactive")}> <div classname="crop-area"> <cropper style={{ height: 280, width: '100%' }} preview=".img-preview" aspectratio={1 / 1} guides={false} src={this.state.src} ref="cropper" crop={this.crop} /> <div classname="row-col"> <div classname="col"><button classname="button button-red" onclick={this.cropimage}>crop</button></div> <div classname="col"><button classname="button button-red" onclick={this.cancelcrop}>cancel</button></div> </div> </div> </div> </div> ); } }); module.exports = myprofileedit;
you can create blob base64 , upload using xmlhttprequest.
var xhr = new xmlhttprequest(); xhr.open('post', '/server', true); xhr.onload = function(e) { ... }; xhr.send(blob);
see here base64 blob conversion: creating blob base64 string in javascript
Comments
Post a Comment