admin管理员组文章数量:1289581
I'm trying to save canvas as an image to the firebase storage. I have read many articles and questions about saving canvas to server, and tried to implement the same with the below code.
function server(){
canvas = document.getElementById("c");
var storageRef = firebase.storage().ref();
var mountainsRef = storageRef.child('mountains.jpg');
var image = new Image();
image.src = canvas.toDataURL("image/png");
var uploadTask = storageRef.child('images/' + "apple").put(image);
uploadTask.on('state_changed', function(snapshot){
// Observe state change events such as progress, pause, and resume
// See below for more detail
}, function(error) {
// Handle unsuccessful uploads
}, function() {
// Handle successful uploads on plete
// For instance, get the download URL: /...
var downloadURL = uploadTask.snapshot.downloadURL;
});
}
But when I run the web app, the console shows error:
FirebaseError: Firebase Storage: Invalid argument in
put
at index 0: Expected Blob or File.
How can I successfully save a canvas to Firebase storage?
I'm trying to save canvas as an image to the firebase storage. I have read many articles and questions about saving canvas to server, and tried to implement the same with the below code.
function server(){
canvas = document.getElementById("c");
var storageRef = firebase.storage().ref();
var mountainsRef = storageRef.child('mountains.jpg');
var image = new Image();
image.src = canvas.toDataURL("image/png");
var uploadTask = storageRef.child('images/' + "apple").put(image);
uploadTask.on('state_changed', function(snapshot){
// Observe state change events such as progress, pause, and resume
// See below for more detail
}, function(error) {
// Handle unsuccessful uploads
}, function() {
// Handle successful uploads on plete
// For instance, get the download URL: https://firebasestorage.googleapis./...
var downloadURL = uploadTask.snapshot.downloadURL;
});
}
But when I run the web app, the console shows error:
FirebaseError: Firebase Storage: Invalid argument in
put
at index 0: Expected Blob or File.
How can I successfully save a canvas to Firebase storage?
Share Improve this question edited Jul 5, 2016 at 19:43 Heretic Monkey 12.1k7 gold badges61 silver badges131 bronze badges asked Jun 17, 2016 at 5:32 pavitranpavitran 8141 gold badge10 silver badges25 bronze badges1 Answer
Reset to default 9Yes this is possible. The problem you are having is that you are trying to upload a dataUrl but firebase's put function only excepts blobs or files. To convert a canvas to a blob use the toBlob
function.
canvas.toBlob(function(blob){
var image = new Image();
image.src = blob;
var uploadTask = storageRef.child('images/' + "apple").put(blob);
});
Edit: changed var uploadTask = storageRef.child('images/' + "apple").put(image);
to var uploadTask = storageRef.child('images/' + "apple").put(blob);
Also not sure if this will work in your case when i tried it I got a tainted canvas error.
What worked for me was the answer to this question How to convert dataURL to file object in javascript?
本文标签: javascriptHow can I save canvas as image to Firebase storageStack Overflow
版权声明:本文标题:javascript - How can I save canvas as image to Firebase storage? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741432731a2378457.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论