Firebase存储不会返回downloadURL

时间:2018-05-21 15:53:27

标签: javascript firebase vuejs2 storage snapshot

我使用此代码更新图片并获取网址,但我无法恢复网址。

似乎返回一些因为它可以进入承诺的回报。

我从这里获得此代码:https://gist.github.com/CristalT/2651023cfa2f36cddd119fd979581893

该代码适用于另一个用户,所以我认为问题在于依赖或在数据库规则中。

我通过了身份验证。

    <template>

      <div>
        <input type="file" multiple accept="image/*" @change="detectFiles($event.target.files)">
        <div class="progress-bar" :style="{ width: progressUpload + '%'}">{{ progressUpload }}%</div>
      </div>

    </template>

    <script>
    import { storage } from '../firebase'
    export default {
      data () {
        return {
          progressUpload: 0,
          file: File,
          uploadTask: '',
          downloadURL: ''
        }
      },
      methods: {
        detectFiles (fileList) {
          Array.from(Array(fileList.length).keys()).map( x => {
            this.upload(fileList[x])
          })
        },
        upload (file) {
          this.uploadTask = storage.ref('imagenes/articulos').put(file);
          this.uploadTask.then(snapshot => {
            this.downloadURL = this.uploadTask.snapshot.downloadURL;
            this.$emit('url', this.downloadURL)
          })
        }
      },
      watch: {
        uploadTask: function() {
          this.uploadTask.on('state_changed', sp => {
            this.progressUpload = Math.floor(sp.bytesTransferred / sp.totalBytes * 100)
          })
        }
      }
    }
    </script>

    <style>
    .progress-bar {
      margin: 10px 0;
    }
    </style>

这是我的package.json:

{
  "name": "vue-change-your-home",
  "version": "1.0.0",
  "description": "Single page make in vue",
  "author": "enrikiko <enrikiko_91@hotmail.com>",
  "private": true,
  "scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "build": "node build/build.js"
  },
  "dependencies": {
    "@fortawesome/fontawesome": "^1.1.8",
    "@fortawesome/fontawesome-free-solid": "^5.0.13",
    "@fortawesome/vue-fontawesome": "0.0.22",
    "bootstrap-vue": "^2.0.0-rc.9",
    "firebase": "^5.0.2",
    "vue": "^2.5.2",
    "vue-router": "^3.0.1",
    "vuefire": "^1.4.5"
  },
  "devDependencies": {
    "autoprefixer": "^7.1.2",
    "babel-core": "^6.22.1",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-loader": "^7.1.1",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-plugin-transform-vue-jsx": "^3.5.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-stage-2": "^6.22.0",
    "chalk": "^2.0.1",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.0",
    "extract-text-webpack-plugin": "^3.0.0",
    "file-loader": "^1.1.4",
    "friendly-errors-webpack-plugin": "^1.6.1",
    "html-webpack-plugin": "^2.30.1",
    "node-notifier": "^5.1.2",
    "optimize-css-assets-webpack-plugin": "^3.2.0",
    "ora": "^1.2.0",
    "portfinder": "^1.0.13",
    "postcss-import": "^11.0.0",
    "postcss-loader": "^2.0.8",
    "postcss-url": "^7.2.1",
    "rimraf": "^2.6.0",
    "semver": "^5.3.0",
    "shelljs": "^0.7.6",
    "uglifyjs-webpack-plugin": "^1.1.1",
    "url-loader": "^0.5.8",
    "vue-loader": "^13.3.0",
    "vue-style-loader": "^3.0.1",
    "vue-template-compiler": "^2.5.2",
    "webpack": "^3.6.0",
    "webpack-bundle-analyzer": "^2.9.0",
    "webpack-dev-server": "^2.9.1",
    "webpack-merge": "^4.1.0"
  },
  "engines": {
    "node": ">= 6.0.0",
    "npm": ">= 3.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

这是存储规则:

 service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

这是我的firebase.js:     从'firebase'导入Firebase

/**
 * Pega aquí los datos de tu proyecto firebase
 */
const firebaseApp = Firebase.initializeApp({
  apiKey: "++++******-sU0qfey9278aBIDP6zo",
  authDomain: "*+****+.firebaseapp.com",
  databaseURL: "https://*****.firebaseio.com",
  projectId: "+++++",
  storageBucket: "**.appspot.com",
  messagingSenderId: "**"
  });

export const db = firebaseApp.database()
export const storage = firebaseApp.storage()
export const auth = firebaseApp.auth()
// export const notif = firebase.messaging()

在终端这是我得到的:

$route
downloadURL:"" --->here shoud get the URL
file:ƒ File()
progressUpload:100
uploadTask:Object
authWrapper_:Object
blob_:Object
chunkMultiplier_:1
errorHandler_:ƒ (error)
error_:null
location_:Object
mappings_:Array[15]
metadataErrorHandler_:ƒ (error)
metadata_:Object

提前非常感谢

2 个答案:

答案 0 :(得分:2)

我有同样的问题,我尝试了很多东西,但我找到的简单解决方案是:

要更改我的package.json中的Firebase版本,我切换到:"firebase": "^4.6.2"

我不确定5.0.2版本发生了什么。即使解决了问题,我仍在寻找变更的文档,也许我们需要以不同的方式使用快照。等着瞧。  祝好运! ;)

- BlisS

更新

问题是snapshop不再拥有版本5的downloadURL,现在你已经使用了属于ref的方法getDownloadURL,就像pushups所说的那样:

const fileRef = firebase.storage()
    .ref("carpeta")
    .child(file.name);

const uploadTask = fileRef.put(file);

uploadTask
    .then(snap=>{
      return fileRef.getDownloadURL()
    })

这也有效: return snap.ref.getDownloadURL()

答案 1 :(得分:1)

您可以使用以下代码将此功能与Firebase 5.2.0配合使用。

UploadTaskPromise产生快照。快照具有ref属性。 ref属性具有getDownloadURL()函数,该函数返回Promise。该Promise会提供可下载的网址,一旦可用即可。

https://firebase.google.com/docs/reference/js/firebase.storage.Reference#getDownloadURL

uploadTask.then((snapshot) => {
    snapshot.ref.getDownloadURL().then((url) => {
        // do something with url here
    });
});