base64转blob Ajax上传文件,服务器端接收不到

时间:2022-08-28 23:42:00

function convertBase64UrlToFileOrBlob(dataURI,type=0) { 
	var arr = dataURI.split(','), mime = arr[0].match(/:(.*?);/)[1],
		  bstr = atob(arr[1]),fileExt = mime.split('/')[1], n = bstr.length, u8arr = new Uint8Array(n);//$window.atob 
	while(n--){
		u8arr[n] = bstr.charCodeAt(n);
	} 
        if(type){
	        return new Blob([u8arr], {type:mime});   
        }else{
            return new File([u8arr],"file_"+Date.parse(new Date())+fileExt,{type:mime});
        }  
    } 


function importImage(myChart,graphName){ 
        //base64 转 blob 
        //var dataURI = encodeURIComponent(myChart.getDataURL("png"));  
        var dataURI = myChart.getDataURL("png");//base64文件 
        var $FileOrBlob = convertBase64UrlToFileOrBlob(dataURI,0); 
        var formData = new FormData();   
        //console.log($FileOrBlob);   
        formData.append("file", $FileOrBlob,"file_"+Date.parse(new Date())+".png");   
        $http({
            url:baseurl + '/data/graph/upload',
            method: "POST", 
            transformRequest: angular.identity,
            headers: {"Content-Type":undefined} 
            data:formData
        }).then(function successCallback(response) {//blob
            // 请求成功执行代码  
            
        }, function errorCallback(response) {
            // 请求失败执行代码 
        });  
    }