使用SDK的WEB版本将图像存储到Firebase存储。文件DOES上传但在尝试获取下载URL时不断收到以下消息
code:"storage/object-not-found"
message:"Firebase Storage: Object 'rainbow_photos/daniel.jpg' does not exist."
name:"FirebaseError"
serverResponse:"{↵ "error": {↵ "code": 404,↵ "message": "Not Found. Could not get object"↵ }↵}"
但文件daniel.jpg将存储在rainbow_photos文件夹中。
以下是我们放置文件的方式:
rainbowPhotoUploader.addEventListener('change', function(e){
//Get file
var file = e.target.files[0];
//Create a storage ref
var storageRef = firebase.storage().ref('rainbow_photos/' + file.name);
//Upload file
storageRef.put(file);
//Get URL and store to pass
storageRef.getDownloadURL().then(function(result){
$('#rainbowPhotoURL').val(result);
});
});
答案 0 :(得分:4)
奥斯汀说的基本上是什么,除非我们很聪明(我们相信我!)我们会在上传后在承诺中返回下载网址,这样你就不必做第二次了取:
rainbowPhotoUploader.addEventListener('change', function(e){
//Get file
var file = e.target.files[0];
//Create a storage ref
var storageRef = firebase.storage().ref('rainbow_photos/' + file.name);
//Upload file
storageRef.put(file).then(function(snapshot){
$('#rainbowPhotoURL').val(snapshot.downloadURL);
});
});
答案 1 :(得分:3)
您在上传后立即获得了下载链接,但尚未完成。
这样做是为了在完成上传后获取链接:
rainbowPhotoUploader.addEventListener('change', function(e){
//Get file
var file = e.target.files[0];
//Create a storage ref
var storageRef = firebase.storage().ref('rainbow_photos/' + file.name);
//Upload file
storageRef.put(file).then(function(result){
//Get URL and store to pass
storageRef.getDownloadURL().then(function(result){
$('#rainbowPhotoURL').val(result);
});
});
});
答案 2 :(得分:0)
成功上传文件后,将第二次使用“然后”。 对不起,我的英语不好。 https://firebase.google.com/docs/storage/web/upload-files
rainbowPhotoUploader.addEventListener('change', function(e) {
var file = e.target.files[0];
var storageRef = firebase.storage().ref('rainbow_photos/' + file.name);
storageRef.put(file).then(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;
console.log('Upload is ' + progress + '% done');
}).then(function() {
// Upload completed successfully, now we can get the download URL
storageRef.getDownloadURL().then(function(downloadURL) {
console.log('File available at', downloadURL);
});
});
});
答案 3 :(得分:0)
对于firebase@6.4(前端SDK)
❌下面的代码将引发错误代码:“ storage / object-not-found” 但是奇怪的是文件实际上已经上传了。
/**
* @param {String} pathToName folder/filename
* @param {Object} file e.target.files[0]
* @returns {Promise} Resolve the URL of the file
*/
export const uploadFile = (pathToName, file) => {
const task = firebase.storage().ref(pathToName).put(file);
return task.snapshot.ref.getDownloadURL();
};
✅firebase@6.4中的工作示例
/**
* @param {String} pathToName folder/filename
* @param {Object} file e.target.files[0]
* @returns {Promise} Resolve the URL of the file
*/
export const uploadFile = (pathToName, file) =>
new Promise((resolve, reject) => {
const task = firebase.storage().ref(pathToName).put(file);
const taskProgress = snapshot => {};
const taskError = reject;
const taskCompleted = () => {
task.snapshot.ref
.getDownloadURL()
.then(resolve)
.catch(reject);
};
task.on("state_changed", taskProgress, taskError, taskCompleted);
});