我正在尝试将图片网址转换为base64图片。我找到了here我正在尝试使用它。
我有以下代码:
public getBase64Image(imgUrl) {
var img = new Image();
img.src = imgUrl;
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
和
img.crossOrigin = "Anonymous";
但是,它输出以下内容:
数据:
我在控制台中收到以下错误:
EXCEPTION:未捕获(承诺):SecurityError:无法执行' toDataURL' on' HTMLCanvasElement':可能无法导出受污染的画布。 错误:无法执行' toDataURL' on' HTMLCanvasElement':可能无法导出受污染的画布。
我的代码必须让我感兴趣。任何人都可以建议如何将网址转换为base64图像?
由于
更新
我将以下行添加到函数中:
<form method="post" role="form" autocomplete="off" id="register-form">
然而,摆脱了错误,现在我得到以下内容:
数据:
答案 0 :(得分:0)
您可以使用XHR和文件阅读器API,它更干净但仅限于browser compatibility。
var pool = mysql.createPool({
connectionLimit : 10,
host : 'localhost',
user : 'root',
password : '',
database : 'myTestDb'
});
var googleAPILink = 'https://roads.googleapis.com/v1/snapToRoads?path='+lastLat+','+lastLong+'|'+currentLat+','+currentLong+'&interpolate=true&key=GOOGLE_MAP_KEY';
console.log(googleAPILink);
var roadResponse = request(googleAPILink, function (error, response, body) {
if (!error && response.statusCode == 200) {
responseData = JSON.parse(body);
for(i = 0; i < responseData.snappedPoints.length; i++) {
var locationArrayObject = responseData.snappedPoints[i];
var locationArrayObjectInsider = (locationArrayObject.location);
var roadLat = locationArrayObjectInsider.latitude;
var roadLong = locationArrayObjectInsider.longitude
var rideStatus = rows2[0].status;
var rideStartedAns = 'n';
if(rideStatus == 's')
{
rideStartedAns = 'y'
}
var post = {
tripid: rideId,
latitude: roadLat,
road_longitude: roadLong,
rideStarted: rideStartedAns,
routeRideCounter: routeCounter,
status: 'y'
};
pool.getConnection(function(err, connectDB4) {
var qry = connectDB4.query('INSERT INTO tbl_rout SET ?', post, function(err5, result5) {
console.log(qry.sql);
connectDB4.release();
});
});
}
}
});