在Firebase存储上显示文本文件中的文本 [英] Displaying text from a text-file on Firebase storage

查看:46
本文介绍了在Firebase存储上显示文本文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Firebase的业余爱好者.我的短期目标是从上传到Firebase的文本文件中读取数据.这是代码(从文档复制粘贴):

I am a total amateur to Firebase. My short-term objective here is to read data from a text file that I uploaded onto Firebase. This is the code (copy-pasted from the docs):

storageRef.child('images/stars.jpg').getDownloadURL().then(function (url) {
  var xhr = new XMLHttpRequest();
  xhr.responseType = 'blob';
  xhr.onload = function(event) {
    var blob = xhr.response;
  };
  xhr.open('GET', (/*here I put the url of the file*/));
  xhr.send();
});

我和这里不一样.如何阅读此文件并将文本复制到页面中?到目前为止,我的代码正确吗?

I am at odds from here. How do I read this file and copy the text into my page? Is my code correct so far?

谢谢.

推荐答案

我花了几天的时间弄清楚如何解决此问题,但后来我意识到,我们又得到了 url 只需在该 url 上创建一个get请求,然后以文本形式获取响应.我实际上是使用angular做到这一点的,但是您可以使用 fetch

I spend days figuring out how to solve this problem, but then I realized, we are getting the url back, so we can just create a get request on that url and get the response as text. I actually did this using angular, but you can do the same with fetch

ngOnInit() {
    const ref = this.storage.ref('array.txt');
    ref.getDownloadURL().subscribe(data => {
        this.http.get(data, { responseType: 'text' as 'json' }).subscribe(text => console.log(text));
      }
    );
  };

使用 fetch

const ref = this.storage.ref('array.txt');
ref.getDownloadURL().subscribe(data => {
  fetch(data)
  .then(function(response) {
    response.text().then(function(text) {
      console.log(text);
    });
  });
});

JSfiddle 可以帮助您.将小提琴的 fetch 中的链接替换为您从firebase获得的 downloadUrl .

A JSfiddle to help you out. Replace the link in the fiddle's fetch with the downloadUrl you're getting from firebase.

更新:如果遇到 CORS 错误,请确保首先遵循这些步骤.

UPDATE: Make sure follow these steps first, if you're getting CORS error.

这篇关于在Firebase存储上显示文本文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆