You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Node.js Admin SDK将File类型图片上传至Firebase Storage

技术问题:Angular + Node.js Firebase Admin SDK 实现图片上传到Firebase Storage

需求说明

前端技术栈为Angular,后端使用适配Node.js的Firebase Admin SDK,需要实现的功能为:用户通过type="file"的<input>控件选择本地图片,Angular端拿到File类型的用户上传图片后,将其发送到Node.js服务器,再由服务器上传到Firebase Storage。

当前前端上传代码

method(imageInput): void {
  const image: File = imageInput.files[0];
  const reader = new FileReader();

  reader.addEventListener('load', (event: any) => {
    const imageData = {
      source: event.target.result,
      file: image
    }

    this.myService.uploadImage(imageData.file).subscribe(
      (res) => {
        // 图片发送成功
      },
      (err) => {
        // 错误处理
      })
  });

  reader.readAsDataURL(image);
}

后端实现遇到的问题

当前尝试的Node.js侧上传代码如下,无法正常执行:

admin.storage().bucket().upload(imageFromAngular, { // 此处存在问题
  destination: "someDestination/",
  contentType: "image/png",
  metadata: {
    contentType: "image/png"
  }
}).then(() => { 
    // 返回成功响应
}).catch(err => {
   // 返回错误响应
});

问题原因:upload方法仅接收图片的本地路径和配置项作为参数,当前场景无法传入本地路径,仅能传入图片本身,查阅官方文档后未找到适配方案,求正确实现方式。

最终可行实现方案

  • 前端改造:在前端的上传方法中调用File类型image对象的arrayBuffer方法,该方法会返回ArrayBuffer类型的Promise,拿到返回值后将其发送到服务端。
  • 后端改造:服务端使用Buffer.from(ArrayBuffer, 'base64')转换接收的图片数据,之后调用save API即可完成上传。
  • 后续操作:后续可通过download API获取已存储的图片。

内容的提问来源于stack exchange,提问作者justchecking

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 09:54:02