Vue中,写了一个上传图片的功能,但是得到路径是base64的图片格式,后台接口需要多张图片数组,不能是base64,试了很多方法无效,请大神们给出建议

解决方案 »

  1.   

    不存base64,后台要存什么呢?
      

  2.   

    他说不可以上传base64,而且我上传接口的时候,好像太大就卡住了
      

  3.   

    图片一般保存在数据库中都是blob字段或者clob字段吧?
      

  4.   

     //对图片进行压缩  
    var image = []; //你要上传的图片数组,存放base64格式的图片     function compress(fileObj, callback){   
            if ( typeof (FileReader) === 'undefined') {    
                console.log("当前浏览器内核不支持base64图标压缩");    
                //调用上传方式不压缩    
                directTurnIntoBase64(fileObj,callback);  
            } else {       
                    var reader = new FileReader();    
                    reader.onload = function (e) { //要先确保图片完整获取到,这是个异步事件   
                     //var src=e.target.result;
                    
                        var image = new Image();  
                        image.src=e.target.result;
                        image.onload = function(){    
                            square = {$pic_compress},   //定义画布的大小,也就是图片压缩之后的像素  
                            canvas = document.createElement('canvas'), //创建canvas元素  
                            context = canvas.getContext('2d'),  
                            imageWidth = Math.round(square*image.width),    //压缩图片的大小  
                            imageHeight = Math.round(square*image.height), 
                            data = '';   
      
       canvas.width = imageWidth;
                            canvas.height = imageHeight;   
                            context.clearRect(0, 0, imageWidth, imageHeight);  //在给定矩形内清空一个矩形   
                                 
                            
                            context.drawImage(this, 0, 0, imageWidth, imageHeight);    
                            var data = canvas.toDataURL('image/jpeg',0.6);    
                            //压缩完成执行回调    
                            image.push(data);
                        };      
                    };  
                
            }  
        }