gpt4 book ai didi

javascript - 文件上传到 Firebase 存储无法正常工作 ("storage/invalid-argument")

转载 作者:行者123 更新时间:2023-11-28 04:07:14 31 4
gpt4 key购买 nike

我试图在上传时显示图像,然后将该图像添加到 firebase。我能够显示图像,但是当我尝试上传时,我收到一条我无法弄清楚的错误消息。这是我的代码

html

<input type="file" accept="image/*"  onchange="showMainImage(this)" />
<br/>
<img id="thumbnil" style="width:40%; margin-top:10px;" src="" alt="image"/>

JS

 function showMainImage(fileInput) {
var files = fileInput.files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
var imageType = /image.*/;
if (!file.type.match(imageType)) {
continue;
}
var img=document.getElementById("thumbnil");
img.file = file;
var reader = new FileReader();
reader.onload = (function(aImg) {
return function(e) {
aImg.src = e.target.result;
};
})(img);
reader.readAsDataURL(file);
}

firebase.auth().onAuthStateChanged((user) => {
if (user) {

database = firebase.database();

var BusinessesId = firebase.auth().currentUser.uid;

//Get file

var filename = files.name;

//Create storage reference
var storageRef = firebase.storage().ref("ProductImages/"+BusinessesId+"/"+filename);

//Upload file
var task = storageRef.put(files).then(function(snapshot) {
console.log('Uploaded', snapshot.totalBytes, 'bytes.');
var url = snapshot.downloadURL;
// productImageUrl.value = url;
console.log('File available at', url);
// [START_EXCLUDE]
}).catch(function(error) {
// [START onfailure]
console.error('Upload failed:', error);
// [END onfailure]
});
// [END oncomplete]


}

})

}

我从控制台得到这个

{t: "storage/invalid-argument", e: "Firebase Storage: Invalid argument in put at index 0: Expected Blob or File.", n: null, r: "FirebaseError"} code : (...) e : "Firebase Storage: Invalid argument in put at index 0: Expected Blob or File."

根据错误消息,我认为该文件的格式不正确。如何修复此错误并上传文件?

最佳答案

put 方法采用非空 Blob、非空 Uint8Array 或非空 ArrayBuffer 作为第一个参数。 https://firebase.google.com/docs/reference/js/firebase.storage.Reference#put

考虑切换到 putString 方法,这样您就可以将任何字符串放入您想要的任何格式。 https://firebase.google.com/docs/reference/js/firebase.storage.Reference#putString

您应该首先转换您的对象。

这是我的实现:我还使用 STATE_CHANGED 来监控图片的上传百分比:

self.uploadPicture = function() {
var userId = self.user().uid;
var storageRef = firebase.storage().ref().child('images/'+userId);
var data = self.fileData().dataURL();
var uploadTask = storageRef.putString(data, 'data_url');
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
function(snapshot) {
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
self.percentage(Math.round(progress));
}, function(error) {
toastr.error(error.code,error.message);
}, function() {
toastr.success("Immagine profilo Aggiornata");
self.savedPicture(true);
});

关于javascript - 文件上传到 Firebase 存储无法正常工作 ("storage/invalid-argument"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46588803/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com