我发帖子请求并收到一系列图片网址。然后我需要将这些图像转换为base64并将它们保存为字符串。以下是转换图像的代码(我使用此引用:http://jsfiddle.net/handtrix/YvQ5y/):
var convertImgToDataURLviaCanvas = function(url, callback, outputFormat){
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var dataURL;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
callback(dataURL);
canvas = null;
};
img.src = url;
};
这是我在收到$ http邮件调用结果后在.then函数中调用的函数:
var base64Image = convertImgToDataURLviaCanvas(resultImages[i], function(base64Img){
console.log(base64Img)
dpd.studentanswers.post({
studentName: "Student" ,
answerImage: 'http://imagesource.com',
questionPrompt: image,
answerImageBase64: 'http://imagesource.com'
}, function(todo, err) {
if (err) {
alert(err.message || (err.errors && err.errors.title));
return;
}});
});
它没有做任何事情。它甚至没有记录任何东西。我非常感谢一些帮助。