为了账号安全,请及时绑定邮箱和手机立即绑定

如何以角度从 Firebase 存储中读取文本文件

如何以角度从 Firebase 存储中读取文本文件

婷婷同学_ 2022-10-21 14:21:10
我在 Firebase 存储中上传文本文件并将一些数据存储在 Firebase 实时数据库中。做上述事情没有问题。此外,我能够获取 firestorage 中存在的所有文件以及文本文件的 URL。 但我无法读取文本文件中的内容。我正在使用下面的代码来获取数据在调用以下代码之前,我在 init 中调用 getfileDetails() 方法将所有数据存储在 fileDetailList 中 this.uploadService.fileDetailList.snapshotChanges().subscribe(      list => {        console.log("before fileListDetails= ");        this.fileListDetails = list.map((item) => {          return item.payload.val();        });        console.log("fileListDetails= " + JSON.stringify(this.fileListDetails)); //logs attached below        this.FileDetailsList = this.fileListDetails;        this.FileDetailsList.forEach((item) => {          console.log("item= " + item.imgUrl);                   // to get the text from file          this.http.get<string>(item.imgUrl).subscribe(            (item11)=>{              console.log("file read= "+item11); //getting error Here                          }          );        });      }    );我的 uploadService.ts 方法fileDetailList:AngularFireList<any>;constructor(private firebase:AngularFireDatabase) { }  getfileDetails()  {    console.log("getfileDetails");    this.fileDetailList=this.firebase.list('FileDetails');  }日志详细信息fileListDetails= [{"caption":"nkn,","category":"hello.txt","imgUrl":"https://firebasestorage.googleapis.com/...."}]当我直接在浏览器中点击 imgUrl 时,我能够获取文本
查看完整描述

1 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

您需要告诉 http 客户端您期望的是纯文本响应,而不是默认的 JSON

请注意,当指定responseTypeJSON 以外的值时,您不需要像this.http.get<MyType>已经指定的那样提供通用参数。

this.http.get(item.imgUrl, {responseType: "text"}).subscribe(...)

参考: https ://angular.io/guide/http#requesting-data-from-a-server


查看完整回答
反对 回复 2022-10-21
  • 1 回答
  • 0 关注
  • 104 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信