Angular / Firebase 上传文件错误:“[object Object]”

Posted

技术标签:

【中文标题】Angular / Firebase 上传文件错误:“[object Object]”【英文标题】:Angular / Firebase upload file Error: "[object Object]" 【发布时间】:2020-05-29 15:41:51 【问题描述】:

编辑:我找到了将文件名放入 ref 所需的解决方案:

this.fireStorage.storage.ref(file.name).put(file);

我正在尝试将文件上传到 FireStorage。 但我被这个错误困住了:

错误错误:“[对象对象]” Angular 13 GedInputComponent.html:6:2 View_GedInputComponent_0 GedInputComponent.html:6 角 13

ERROR CONTEXT Object view: …, nodeIndex: 8, nodeDef: …, elDef: …, elView: … GedInputComponent.html:6:2 View_GedInputComponent_0 GedInputComponent.html:6 角 13

这是我的代码

<mat-dialog-content class="content">
  <input type="file" (change)="detectFile($event)"/>
</mat-dialog-content>
<mat-dialog-actions class="buttons">
  <button mat-raised-button (click)="addFile()">Add</button>
</mat-dialog-actions>

我在这里得到文件:

export class GedInputComponent 
  selectedFile: File;

  addFile() 
    this.gedService.uploadFile(this.selectedFile);
    this.dialogRef.close();
  
  detectFile(event) 
    this.selectedFile = event.target.file;
  

这里我尝试上传文件:

export class GedService 
  constructor(private fireStorage: AngularFireStorage)  
  public uploadFile(file: File): void 
    this.fireStorage.storage.ref().put(file); // When i remove this line there is no error
  

【问题讨论】:

我认为你应该这样做this.selectedFile = event.target.file[0]; 我已经尝试过了,但它不起作用。不幸的是我有同样的错误 您看到@PierreDuc 对我的回答所做的更正了吗?有一个错字。谢谢皮埃尔杜克。 【参考方案1】:

我认为你应该这样做:

  detectFile(event) 
    this.selectedFile = event.target.files[0];
  

查看Uploading files with upload 和Using files from web applications。

【讨论】:

以上是关于Angular / Firebase 上传文件错误:“[object Object]”的主要内容,如果未能解决你的问题,请参考以下文章

在 Angular 11 网站上使用 Firestore 和 Firebase 存储进行图像上传功能导致来自 Firebase 的不安全规则通知

HOC 反应 redux firebase。 (错误:上传文件需要 Firebase 存储)

上传位于 Angular 项目目录中的文件/图像

如何修复 React/Redux/Firebase 错误上传文件到存储

将文件上传到 Firebase 存储时出现身份验证错误

Angular Firebase 存储,将用户输入属性分配给实时数据库