小编lue*_*rro的帖子

如何将 base64 字符串作为图像提供?

我有一个来自数据库的 Base64 字符串,我想通过 GET 提供该字符串。我知道必须首先将其转换为缓冲区,然后我需要将 Content-Type 设置为 image/png (因为我的图像是 png)。问题是图像没有显示。如果有帮助,我将使用 Nest JS 作为框架。

这是我尝试提供的 base64 字符串: https: //pastebin.com/kbvGLXSp

我尝试通过 Chrome 和 Postman 运行端点,得到了相同的结果。图像未显示

// From auth.controller.ts
@Get('avatar')
@Header('Content-Type', 'image/png')
  async getAvatar(){
    return await this.authServ.getAvatar();
  }


// From auth.service.ts
async getAvatar() {
    const db = this.AUTH_REPOSITORY;
    const user = await db.findOne({where: {id: 1}});
    return Buffer.from(user.avatar, 'base64');
}
Run Code Online (Sandbox Code Playgroud)

图像应该显示,但没有显示。我在 chrome 中得到类似的东西:( https://i.ibb.co/kMWJxn2/Capture.png )

base64 node.js

1
推荐指数
1
解决办法
3774
查看次数

在nestjs中重试rxjs

import { HttpService, Injectable } from '@nestjs/common';
import { retry } from 'rxjs/operators';

@Injectable()
export class XXXService {
  constructor(private http: HttpService) {}

  predictAll() {
    return this.http
      .post('https://xxxxx/xxxxx')
      .pipe(retry(5));
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我有以下代码来订阅上面的可观察值

import { Inject, Injectable } from '@nestjs/common';
import { XXXService } from '@api/services/XXX.service';

@Injectable()
export class YYYService {

  constructor(
    private readonly xxxService: XXXService
  ) {
    this.predictAll();
  }

  async predictAll() {
    await this.xxxService.predictAll().subscribe(
      ({ data }) => {
        console.log(data);
      },
      err => {
        console.log('error');
      }
    );
  }
}

Run Code Online (Sandbox Code Playgroud)

然后我尝试关闭我的互联网连接,经检查,“错误”仅在控制台中打印一次,这意味着我的可观察值根本没有重试。我这样做有什么问题吗?

rxjs nestjs

0
推荐指数
1
解决办法
5116
查看次数

标签 统计

base64 ×1

nestjs ×1

node.js ×1

rxjs ×1