2016-06-28 35 views
0

我已經使用反饋裁剪js創建了裁剪功能。我需要將圖像作爲文件類型發送。我能夠獲得裁剪圖像的base64字符串。當我提交裁剪圖像需要發送到一個文件類型。在這裏我已經包含了裁剪功能的代碼。如何將裁剪區域圖像作爲文件發送?在React裁剪器js中將base64轉換爲文件類型?

這裏是我的代碼,

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; 

回答

0

您可以創建一個的base64 Blob和上傳使用XMLHttpRequest的。

var xhr = new XMLHttpRequest(); 
xhr.open('POST', '/server', true); 
xhr.onload = function(e) { ... }; 
xhr.send(blob); 

看到這裏的base64左右到BLOB轉換:Creating a Blob from a base64 string in JavaScript