jam*_*phy 4 react-native firebase-storage react-native-firebase
我试图找到一种有效的方法来在上传图像后立即在 firebase 中获取图像的 url。我想避免编写一个完全独立的函数......相反,我想将它包含在承诺链中。请参阅下面的代码
import storage from '@react-native-firebase/storage';
storage()
.ref('path/to/remote/folder')
.putFile('uri/of/local/image')
.then(() => {
//Id like to use getDownloadUrl() function here;
})
Run Code Online (Sandbox Code Playgroud)
您可以使用创建承诺链await,然后轻松获取下载网址,如下所示:
/**
* Upload the image to the specific firebase path
* @param {String} uri Uri of the image
* @param {String} name Name of the image
* @param {String} firebasePath Firebase image path to store
*/
const uploadImage = async (uri, name, firebasePath) => {
const imageRef = storage().ref(`${firebasePath}/${name}`)
await imageRef.putFile(uri, { contentType: 'image/jpg'}).catch((error) => { throw error })
const url = await imageRef.getDownloadURL().catch((error) => { throw error });
return url
}
Run Code Online (Sandbox Code Playgroud)
现在您可以按如下方式调用该函数:
const uploadedUrl = await uploadImage('uri/of/local/image', 'imageName.jpg', 'path/to/remote/folder');
Run Code Online (Sandbox Code Playgroud)
现在uploadedUrl将包含上传图像的 url。
| 归档时间: |
|
| 查看次数: |
6504 次 |
| 最近记录: |