Image 调整图像大小并在couchdb中上载,或将url blob上载到couchdb

Image 调整图像大小并在couchdb中上载,或将url blob上载到couchdb,image,upload,resize,couchdb,attachment,Image,Upload,Resize,Couchdb,Attachment,我需要上传附加到couchdb文件大小的图像。现在,我没有调整图像大小,只是使用以下代码上传图像: function attachFile(event) { event.preventDefault(); var form_data = {}; $("form.attach-file :file").each(function() { form_data[this.name] = this.value; }); if (!form_data._attac

我需要上传附加到couchdb文件大小的图像。现在,我没有调整图像大小,只是使用以下代码上传图像:

function attachFile(event) {
   event.preventDefault();
   var form_data = {};
   $("form.attach-file :file").each(function() {
      form_data[this.name] = this.value;
   });
   if (!form_data._attachments || form_data._attachments.length == 0) {
      alert("Please select a file to upload.");
      return;
   }

   var id = $("#ant-show").data("doc")._id;
   $(this).ajaxSubmit({
      url: "db/" + $.couch.encodeDocId(id),
      success: function(resp) {
         $('#modal-attach').modal("hide");
         helios_link(id);
      } 
   });
}
我正在使用以下代码对图像进行重新zise,但无法上传图像:

function attachFile(event) {
    function isImage (str) {
        return str.match(/(^data:image\/.*,)|(\.(jp(e|g|eg)|gif|png|bmp|webp|svg)((\?|#).*)?$)/i);
    }
    function resizeAndUpload(file, callback, progress)
    {
        var reader = new FileReader();
        reader.onloadend = function() {
            var tempImg = new Image();
            tempImg.onload = function() {
                var MAX_WIDTH = 500;
                var MAX_HEIGHT = 500;
                var tempW = tempImg.width;
                var tempH = tempImg.height;
                if (tempW > tempH) {
                    if (tempW > MAX_WIDTH) {
                        tempH *= MAX_WIDTH / tempW;
                        tempW = MAX_WIDTH;
                    }
                } else {
                    if (tempH > MAX_HEIGHT) {
                        tempW *= MAX_HEIGHT / tempH;
                        tempH = MAX_HEIGHT;
                    }
                }
                var resizedCanvas = document.createElement('canvas');
                resizedCanvas.width = tempW;
                resizedCanvas.height = tempH;
                var ctx = resizedCanvas.getContext("2d");
                ctx.drawImage(this, 0, 0, tempW, tempH);
                var dataURL = resizedCanvas.toDataURL("image/jpeg");
                var file = dataURLtoBlob(dataURL);
                var fd = $("#upload");
                fd.append("_attachments", file);
                var id = $("#ant-show").data("doc")._id;
                console.log(fd);
                fd.ajaxSubmit({
                    url: "db/" + $.couch.encodeDocId(id),
                    success: function(resp) {
                        $('#modal-attach').modal("hide");
                        helios_link(id);
                    }         
                });
            };
            tempImg.src = reader.result;
        }
        reader.readAsDataURL(file);
    }

    function dataURLtoBlob(dataURL) {
        // Decodifica dataURL
        var binary = atob(dataURL.split(',')[1]);
        // Se transfiere a un array de 8-bit unsigned
        var array = [];
        var length = binary.length;
        for(var i = 0; i < length; i++) {
            array.push(binary.charCodeAt(i));
        }
        // Retorna el objeto Blob
        return new Blob([new Uint8Array(array)], {type: 'image/jpeg'});
    }

    function uploaded(response) {
        // Código siguiente a la subida
    }

    function progressBar(percent) {
        // Código durante la subida
    }
    event.preventDefault();
    console.clear();
    var files = document.getElementById('_attachments').files;
    console.log(files);
    resizeAndUpload(files[0], uploaded, progressBar);

}
函数附加文件(事件){
函数isImage(str){
返回str.match(/(^data:image\/.*))|(\(jp(e | g | eg)| gif | png | bmp | webp | svg)(\?| | | | | | | | | | | | | |;
}
函数resizeAndUpload(文件、回调、进度)
{
var reader=new FileReader();
reader.onloadend=函数(){
var tempImg=新图像();
tempImg.onload=函数(){
var MAX_WIDTH=500;
var最大高度=500;
var tempW=tempImg.width;
var tempH=临时高度;
if(tempW>tempH){
如果(速度>最大宽度){
tempH*=最大宽度/tempW;
tempW=最大宽度;
}
}否则{
如果(速度>最大高度){
tempW*=最大高度/tempH;
tempH=最大高度;
}
}
var resizedCanvas=document.createElement('canvas');
resizedCanvas.width=tempW;
resizedCanvas.height=tempH;
var ctx=resizedCanvas.getContext(“2d”);
ctx.drawImage(this,0,0,tempW,tempH);
var dataURL=resizedCanvas.toDataURL(“图像/jpeg”);
var file=dataURLtoBlob(dataURL);
var fd=$(“#上传”);
fd.追加(“_附件”,文件);
变量id=$(“#ant show”).data(“doc”)。_id;
控制台日志(fd);
fd.ajaxSubmit({
url:“db/”+$.coach.encodeDocId(id),
成功:功能(resp){
$('#模态附加')。模态(“隐藏”);
helios_链路(id);
}         
});
};
tempImg.src=reader.result;
}
reader.readAsDataURL(文件);
}
函数dataURLtoBlob(dataURL){
//decodicadataurl
var binary=atob(dataURL.split(',)[1]);
//Se转换一个8位无符号的un数组
var数组=[];
var length=二进制长度;
对于(变量i=0;i
有人知道如何改进代码以使其正常工作吗?事实上,我希望有两种不同的解决方案,一种帮助我改进代码,另一种帮助我获得关于如何将URL BLOB作为附件上传到couchdb文档中的说明