简体   繁体   English

如何将文件上传到 Firebase 存储并显示下载 URL?

[英]how can i upload file to firebase storage and display the download URL?

i want the file to be uploaded and the download url to be printed here is the code iv'e used for that我希望上传文件并在此处打印下载 url 是我为此使用的代码

<script>
      var file = document.getElementById('file');
      var storageRef = firebase.storage().ref();

// Create the file metadata
var metadata = {
  contentType: 'image/jpeg'
};
var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
  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');
    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;
    }
  }, function(error) {
  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;
  }
}, function() {
  // Upload completed successfully, now we can get the download URL
  uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
    console.log('File available at', downloadURL);
  });
});

      </script>

The file is not uploading the console shows the error as follows文件没有上传控制台显示错误如下在此处输入图片说明

please help me with the correction of code that uploads the file and displays the download url请帮我更正上传文件并显示下载网址的代码

负责文件上传的表单元素需要如下所示:

<form enctype="multipart/form-data" action="/upload/file" method="post">

暂无
暂无

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

相关问题 如何获取firebase文件存储的下载地址 - How to get the download Url of firebase file storage 我正在尝试将图像上传到 firebase 存储并获取下载 url - I'm trying to upload an image to firebase storage and get the download url 如何在 React js 中从 firebase 存储下载文件? - How can I download a file from firebase storage in react js? 如何将图像上传到 firebase 存储,然后返回下载 url? - How to upload an image to firebase storage and then return the download url? 在 Firebase 存储上完成上传后如何获取下载 URL - How do i get download URL once upload is done on firebase storage 如果我知道它将以什么名称存储,如何从 firebase 存储文件中获取图像下载 url? - How can I get image download url from firebase storage file, if I know what name it will be stored under? 将图像上传到 Firebase 存储后如何检索下载 Url,以便我可以将其保存到 Firebase 数据库? - How to retrieve the download Url after uploading image to Firebase storage, so that I can save it to Firebase database? 如何将文件上传到 Firebase 存储? - How do I upload a file to Firebase storage? 如何从 firebase 存储自动下载文件,而不是使用 url - Javascript - How to download a file from firebase storage automaticly and not with the url - Javascript 成功将图像上传到 Firebase 存储后,Firebase 获取下载 URL - Firebase get Download URL after successful image upload to firebase storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM