简体   繁体   English

如何将图像上传到 firebase 存储,然后返回下载 url?

[英]How to upload an image to firebase storage and then return the download url?

Im trying to upload an image to firebase storage in a react-native project, then return the download URL once upload is complete.我尝试将图像上传到 react-native 项目中的 firebase 存储,然后在上传完成后返回下载 URL。

this is what I tried: EDITED:这就是我尝试过的:编辑:

const uploadImg = async (dispatch, uri, uid, mime = 'image/png') => {
    console.log('Starting image upload...');

    dispatch({ type: types.UPLOAD_IMG, info: 'Uploading profile image...' });
    const uploadUri = Platform.OS === 'ios' ? uri.replace('file://', '') : uri;

    return (imageRef = firebase
        .storage()
        .ref(uid)
        .child('profileImg')
        .putFile(uploadUri, { contentType: 'image/png' })
        .catch((err) => {
            uploadImgFail(dispatch, err.message);
        }));
};

part of implementation:部分实施:

uploadImg(dispatch, img, userCredential.user.uid)
    .then((imgRef) => {
        const imgUrl = imgRef.getDownloadURL();
        console.log('uploaded Image URL: ' + imgUrl)
        uploadImgSuccess(dispatch, 'Profile image upload successful...');
        // rest of action
}

the problem is the download URL isn't returned by the function, I get unhandled promise rejection: "can't find variable: imgUrl"问题是该函数没有返回下载 URL,我得到未处理的承诺拒绝:“找不到变量:imgUrl”

Any help on achieving this with react-native-firebase ?使用react-native-firebase实现这一目标有什么帮助吗?

You will need to actually return a value from uploadImg .您实际上需要从uploadImg返回一个值。 Right now it's returning nothing.现在它什么都不返回。 Add a return keyword before imageRef.putFile() to return the promise that its chained promise returns.imageRef.putFile()之前添加return关键字以返回其链接的承诺返回的承诺。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 成功将图像上传到 Firebase 存储后,Firebase 获取下载 URL - Firebase get Download URL after successful image upload to firebase storage 我正在尝试将图像上传到 firebase 存储并获取下载 url - I'm trying to upload an image to firebase storage and get the download url Firebase 存储图像上传 - 上传后返回图像 URL 的函数 - Firebase Storage Image upload - Function to return the Image URL after uploading it 如何将文件上传到 Firebase 存储并显示下载 URL? - how can i upload file to firebase storage and display the download URL? 如何从 Firebase 存储中获取图像的下载 url? - How to get download url for a Image from Firebase Storage? 在 React.js 中使用 Dropzone 时如何将图像上传到 Firebase 存储并获取图像的下载 URL - How to Upload Images to Firebase Storage and Get Image's Download URL's When Using Dropzone in React.js 检索 Firebase 存储图像的下载 url 时出现问题 - Problem retrieving download url of Firebase storage image 将图像从 URL 上传到 Firebase 存储 - Upload image from URL to Firebase Storage 当我将图像上传到Firebase存储并检索下载URL并显示在img标签中时,它显示多次 - When I upload an image to firebase storage and retriving the download url and displaying in img tag but it is showing multiple times 当我上传图像 firebase 存储 getdownloadUrl() 将下载 url 放入 Firestore Collections 但未在 React JS 中设置 - When I upload a image firebase storage getdownloadUrl() put download url In Firestore Collections but not set it in React JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM