我一直在尝试重新实现像Mozilla Hacks网站上的 HTML5图像上传器,但它适用于WebKit浏览器.部分任务是从canvas对象中提取图像文件并将其附加到FormData对象以进行上载.
问题是虽然canvas具有toDataURL返回图像文件表示的函数,但FormData对象仅接受来自File API的 File或Blob对象.
Mozilla解决方案使用以下仅限Firefox的功能canvas:
var file = canvas.mozGetAsFile("foo.png");
Run Code Online (Sandbox Code Playgroud)
...在WebKit浏览器上不可用.我能想到的最好的解决方案是找到一种方法将数据URI转换为File对象,我认为它可能是File API的一部分,但我不能为我的生活找到一些东西.
可能吗?如果没有,任何替代方案?
谢谢.
我使用http://www.dropzonejs.com/实现了一个dropzone页面
我在调整大小功能时遇到问题.如果长宽比错误,我的图像会不断裁剪.
我想知道两件事:
我已经通过调整css实现了2,但我想知道是否有更好的方法使用dropzone代码.
如果有人有1,那么1的例子会非常有用.谢谢,马特.
我想在这里做的是在Dropzone.js将丢弃的图像发送到服务器之前,使用cropper.js(fengyuanchen脚本)出现一个模态,这样用户就可以裁剪图像,当裁剪图像时,使用Dropzone.js发送它到服务器.
因此,当我使用函数fileToBase64更改#cropbox的图像src并使用函数cropper('replace')替换cropper的图像时,它会一直显示default.jpg图像,而不是从用户上传的新图像
HTML
<div class="wrapper-crop-box">
<div class="crop-box">
<img src="default.jpg" alt="Cropbox" id="cropbox">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
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() { …Run Code Online (Sandbox Code Playgroud) 我正在尝试调整缩略图中的图像大小以修复框大小.我试过这个:
Dropzone.options.myAwesomeDropzone = {
maxFiles: 20,
maxFilesize: 2,
maxThumbnailFilesize: 20,
acceptedFiles: 'image/*,.jpg,.png,.jpeg',
thumbnailWidth:"250",
thumbnailHeight:"250",
init: function() {
var dropzone = this, xhrs = [];
$.each(uploadedImages, function (index, path) {
var xhr = new XMLHttpRequest();
xhr.open('GET', path);
xhr.responseType = 'blob';//force the HTTP response, response-type header to be blob
xhr.onload = function() {
var file = new File([xhr.response], '', {'type': xhr.response.type});
//dropzone.addUploadedFile(file);
dropzone.emit("addedfile", file);
dropzone.emit("thumbnail", file, path);
dropzone.emit("complete", file);
};
xhrs.push(xhr);
xhr.send();
});
this.options.maxFiles = this.options.maxFiles - uploadedImages.count;
}
};
Run Code Online (Sandbox Code Playgroud)
这是原始图像的外观:http …