1
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.

Lambdaで作成したExcelファイルをダウンロードする

Last updated at Posted at 2023-05-09

環境

React:16.8.5

やり方

lambdaで作成したExcelファイルをbase64に変換しレスポンスとして返します。
それをReactでBlobに変換してダウンロードしました

const data = 'base64のデータ';
const binary = Buffer.from(data, 'base64')
const decoded = new Blob([Uint8Array.from(binary)], {
	type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
const url = window.URL.createObjectURL(decoded);
const a = document.createElement("a");
a.href = url;
a.download = "sample.xlsx";
a.click();
window.URL.revokeObjectURL(url);

base64から直接Blobに変換するのではなく、まずはUint8Arrayに変換します。
→そしてUint8ArrayをBlobに変換し、aタグを使ってダウンロードします。

参考にしていただければ幸いです。

1
0
1

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
1
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?