文件url到数据uri转换

时间:2018-01-09 03:16:46

标签: angularjs angular ionic-framework ionic2

我通过http.post请求从服务器获取文件URL。我需要将image转换为base64字符串以进行数据库存储。我使用角度进行离子2+应用的解决方案

convertToDataURLviaCanvas(url, outputFormat){
    return new Promise( (resolve, reject) => {
        let img = new Image();
        img.crossOrigin = 'Anonymous';
        img.onload = function(){
            let canvas = <HTMLCanvasElement> document.createElement('CANVAS'),
            ctx = canvas.getContext('2d'),
            dataURL;
            canvas.height = this.height;
            canvas.width = this.width;
            ctx.drawImage(this, 0, 0);
            dataURL = canvas.toDataURL(outputFormat);
            //callback(dataURL);
            canvas = null;
            resolve(dataURL); 
        };
        img.src = url;
    });
}

并使用:

致电
this.convertToDataURLviaCanvas(someUrl, "image/jpeg")
.then( base64Img => {
   //do whatever you need here, with the base64 data
})

所以我需要使用angularjs转换离子等效代码..

0 个答案:

没有答案