![](/img/trans.png)
[英]How to get reusable url for a picture in firebase storage that has a known path?
[英]How to upload a picture in firebase storage and get it url in firebase document field
我使用圖像選擇器庫成功上傳了圖片,但無法在 firebase 存儲中上傳。
這是我的代碼
launchImageLibrary(options, (response) => {
console.log('Response = ', response);
if (response.didcancel) {
console.log('User cancelled image picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.custombutton);
} else {
//Convert image to base64
source = JSON.stringify(response.assets[0].uri);
console.log('Image in Base64 is ', JSON.stringify(response.assets[0].uri));
RNFS.readFile(response.assets[0].uri, 'base64')
.then((res) => {
console.log(res); // The base64 string
})
.catch((err) => {
console.log(err);
});
}
});
}
它被記錄在案。 獲得下載.update
后,只需將其更新為現有的 firestore 文檔,或set
為創建一個新文檔。
// Create the file metadata
var metadata = {
contentType: 'image/jpeg'
};
// Upload file and metadata to the object 'images/mountains.jpg'
var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);
// Listen for state changes, errors, and completion of the upload.
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
(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');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
},
(error) => {
// A full list of error codes is available at
// https://firebase.google.com/docs/storage/web/handle-errors
switch (error.code) {
case 'storage/unauthorized':
// User doesn't have permission to access the object
break;
case 'storage/canceled':
// User canceled the upload
break;
// ...
case 'storage/unknown':
// Unknown error occurred, inspect error.serverResponse
break;
}
},
() => {
// Upload completed successfully, now we can get the download URL
uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => {
console.log('File available at', downloadURL);
});
}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.