【发布时间】:2015-06-06 10:21:54
【问题描述】:
我在这里要做的是在 Dropzone.js 将丢弃的图像发送到服务器之前,会出现一个带有cropper.js(fengyuanchen 脚本)的模式,以便用户可以裁剪图像,当图像被裁剪时,将其发送Dropzone.js 到服务器。
因此,当我使用函数 fileToBase64 更改 #cropbox 的图像 src 并使用函数 cropper('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'
});
});
【问题讨论】:
标签: jquery image base64 crop dropzone.js