0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Next.jsのapi routeでbase64化されてものをデータとしてresponseしたい

Posted at
const data = // base64化されたデータ
res.setHeader("Content-Type", "application/pdf") // データの種類に合わせる
res.setHeader("Content-Disposition", "inline;") // どう表示したいかに合わせる
res.status(200).send(Buffer.from(data, "base64")) // bufferを直接送れる

Data URL

[<mediatype>][;base64],<data>のように

  • 最初にヘッダーのようなものがあり
  • ,
  • base64データ

という構成の場合は、カンマ以降の部分だけを取り出す必要がある。

cosnt dataURL = "..."
const data = dataURL.split(",")[1]

atob

巷によくあるサンプルコードだとatobを使っているが、nodeではdeprecatedになっているので、Buffer.fromを使ってBufferに変換する。そうするとBufferは直接send出来る。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?