如何从 JSON 响应中解码客户端的 Base64 文件?
How to decode a Base64 file on the client side from JSON response?
我有一个在服务器端呈现文件的应用程序。我通过 json 将其作为“base64”字符串传递。我需要在客户端解码和下载文件。
我把代码以简化的形式呈现出来,留下与问题相关的部分。
这是快递服务器上的路线:
let xlsx = require('node-xlsx').default;
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
const data = [[1, 2, 3], ['a', 'b', 'c']];
var buffer = xlsx.build([{ name: "mySheetName", data: data }])
.toString('base64');
res.json(buffer);
});
module.exports = router;
客户端上的 React 组件:
import React, { useEffect } from 'react';
import axios from 'axios';
const First = () => {
useEffect(() => {
axios({
url: 'http://localhost:5000/excel',
method: 'GET',
responseType: 'blob',
}).then(res => {
const url = window.URL.createObjectURL(new Blob([res.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'data.xlsx');
document.body.appendChild(link);
link.click();
});
}, []);
return (
<div>
<h3>First page</h3>
<hr className="gold" />
</div>
);
};
export default First;
此致。
使用atob
查看快速示例
let myData = "YQ=="
console.log(atob(myData)); // a
这帮助我解码并下载了 xlsx 文件:
import React, { useEffect } from 'react';
import axios from 'axios';
import download from 'downloadjs';
const First = () => {
useEffect(() => {
axios.get('http://localhost:5000/excel')
.then(res => {
download(atob(res.data), 'data.xlsx', { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
});
}, []);
return (
<div>
<h3>First page</h3>
<hr className="gold" />
</div>
);
};
export default First;
我用了download.js
我有一个在服务器端呈现文件的应用程序。我通过 json 将其作为“base64”字符串传递。我需要在客户端解码和下载文件。 我把代码以简化的形式呈现出来,留下与问题相关的部分。
这是快递服务器上的路线:
let xlsx = require('node-xlsx').default;
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
const data = [[1, 2, 3], ['a', 'b', 'c']];
var buffer = xlsx.build([{ name: "mySheetName", data: data }])
.toString('base64');
res.json(buffer);
});
module.exports = router;
客户端上的 React 组件:
import React, { useEffect } from 'react';
import axios from 'axios';
const First = () => {
useEffect(() => {
axios({
url: 'http://localhost:5000/excel',
method: 'GET',
responseType: 'blob',
}).then(res => {
const url = window.URL.createObjectURL(new Blob([res.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'data.xlsx');
document.body.appendChild(link);
link.click();
});
}, []);
return (
<div>
<h3>First page</h3>
<hr className="gold" />
</div>
);
};
export default First;
此致。
使用atob
查看快速示例
let myData = "YQ=="
console.log(atob(myData)); // a
这帮助我解码并下载了 xlsx 文件:
import React, { useEffect } from 'react';
import axios from 'axios';
import download from 'downloadjs';
const First = () => {
useEffect(() => {
axios.get('http://localhost:5000/excel')
.then(res => {
download(atob(res.data), 'data.xlsx', { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
});
}, []);
return (
<div>
<h3>First page</h3>
<hr className="gold" />
</div>
);
};
export default First;
我用了download.js