0

我在 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 时,我能够获取文本

4

1 回答 1

1

您需要告诉 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

于 2020-06-28T10:18:27.377 回答