[英]Convert image file to Blob
I have the following code: 我有以下代码:
var file = document.getElementById("image1");
var filename = file.files[0].name;
//console.log("Archivo: " + filename);
var storageRef = firebase.storage().ref();
var ref = storageRef.child('imagenes/'+filename);
var reader = new FileReader();
var rawData;
reader.onload = function(e) {
rawData = reader.result;
}
reader.readAsBinaryString(file);
ref.put(rawData).then(function(snapshot){
});
File -> get the data file input (an image). File->获取数据文件输入(图像)。
And I want convert this file to blob in the moment that the form is send, to upload to firebase storage the image in the format BLOB, how I can do this? 我想在发送表单时将此文件转换为blob,然后将BLOB格式的图像上传到Firebase存储,我该怎么做? I was used the fileReader, but always return the same error:
我曾经使用过fileReader,但是总是返回相同的错误:
'readAsBinaryString' on 'FileReader': parameter 1 is not of type 'Blob'.
'FileReader'上的'readAsBinaryString':参数1不是'Blob'类型。
How do convert the file image to a blob? 如何将文件图像转换为Blob?
You can't simply set rawData
inside the onload function like you're doing. 您不能像您那样简单地在onload函数中设置
rawData
。 By the time ref.put()
is run, the onload function will not have been called, so rawData will ALWAYS be undefined. 到
ref.put()
运行时,将不会调用onload函数,因此rawData将始终是未定义的。 ref.put()
takes in the file as its parameter, so simply use ref.put(file.files[0])
(No reader required): ref.put()
将文件作为参数,因此只需使用ref.put(file.files[0])
(无需阅读器):
var file = document.getElementById("image1");
var filename = file.files[0].name;
//console.log("Archivo: " + filename);
var storageRef = firebase.storage().ref();
var ref = storageRef.child('imagenes/'+filename);
ref.put(file.files[0]).then(...);
See also the very common: How do I return the response from an asynchronous call? 另请参见非常常见的内容: 如何从异步调用返回响应?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.