我想在這裏做的是前Dropzone.js丟棄的圖像發送到服務器,一個模式出現帶有cropper.js(fengyuanchen腳本),使用戶可以裁剪圖像和圖像被裁剪時,將其與Dropzone.js一起發送到服務器。使用cropper.js發送圖像到服務器
因此,當我用函數fileToBase64更改#cropbox的圖像src並用函數裁剪器('replace')替換裁剪器的圖像時,它會一直顯示default.jpg圖像,而不是從上傳的新圖像用戶
HTML
<div class="wrapper-crop-box">
<div class="crop-box">
<img src="default.jpg" alt="Cropbox" id="cropbox">
</div>
</div>
JS:
function fileToBase64(file) {
var preview = document.querySelector('.crop-box img');
var reader = new FileReader();
reader.onloadend = function() {
preview.src = reader.result;
}
if (file) {
reader.readAsDataURL(file);
} else {
preview.src = "";
}
}
$(function() {
Dropzone.options.avtDropzone = {
acceptedFiles: 'image/*',
autoProcessQueue: true,
paramName: 'file',
maxFilesize: 2,
maxFiles: 1,
thumbnailWidth: 200,
thumbnailHeight: 200,
accept: function(file, done) {
fileToBase64(file);
$('#cropbox').cropper('replace', $('#cropbox').attr('src'));
$('.wrapper-crop-box').fadeIn();
done();
},
init: function() {
this.on("addedfile", function(file) {
if (this.files[1]!=null){
this.removeFile(this.files[0]);
}
});
}
};
$('#cropbox').cropper({
aspectRatio: 1/1,
resizable: false,
guides: false,
dragCrop: false,
autoCropArea: 0.4,
checkImageOrigin: false,
preview: '.avatar'
});
});
工程令人驚歎..除了它沒有上傳裁剪的圖像,但我的原始:/把我的代碼在小提琴https://jsfiddle.net/CanR/1b9dy5cd/所以每一步是或似乎很好,只有最終結果不是裁剪正方形而是原始 – Can 2016-01-25 01:54:15