将字符串 base64 转换为 Object JS 文件
Convert string base64 to Object JS file
我需要在数据库中保存一个JS文件,然后检索这个文件。
保存文件转string base64时,如何读取这个base64文件并转回原来的格式(JS文件)?
我像这样用 axios 保存到数据库中:
let formData = new FormData();
formData.append('file', file);
const create = (formData, onUploadProgress) => {
return http.post(
'/api/v1/maps/upload',
formData,
{
headers: {
'Content-Type': 'multipart/form-data',
},
onUploadProgress,
}
);
};
原文件:
export default {
label: "Test file JS",
viewBox: "0 0 800 1120",
locations: [
{
id: "01",
path:
"M 445.73 747.67 205.6 533.44 574.22 1 797.73 195.63 799 204.74 535.49 492.74 591.82 587.22 445.73 747.67 Z"
},
{
id: "02",
path: "M 1 818.38 203.08 530.9 443.39 749.75 105.29 1119.38 1 818.38 Z"
}
]
};
从 base64 数据库中检索到的文件:
ZXhwb3J0IGRlZmF1bHQgewpsYWJlbDogIlRlc3QgZmlsZSBKUyIsCiAgdmlld0JveDogIjAgMCA4MDAgMTEyMCIsCiAgbG9jYXRpb25zOiBbCiAgICB7CiAgICAgIGlkOiAiMDEiLAogICAgICBwYXRoOgogICAgICAgICJNIDQ0NS43MyA3NDcuNjcgMjA1LjYgNTMzLjQ0IDU3NC4yMiAxIDc5Ny43MyAxOTUuNjMgNzk5IDIwNC43NCA1MzUuNDkgNDkyLjc0IDU5MS44MiA1ODcuMjIgNDQ1LjczIDc0Ny42NyBaIgogICAgfSwKICAgIHsKICAgICAgaWQ6ICIwMiIsCiAgICAgIHBhdGg6ICJNIDEgODE4LjM4IDIwMy4wOCA1MzAuOSA0NDMuMzkgNzQ5Ljc1IDEwNS4yOSAxMTE5LjM4IDEgODE4LjM4IFoiCiAgICB9CiAgXQp9Ow==
感谢您的帮助
为了将 base64 解码为字符串,您需要使用 atob
.
<html>
<body>
<script id="output" type="module"></script>
<script type="module">
const content = atob("ZXhwb3J0IGRlZmF1bHQgewpsYWJlbDogIlRlc3QgZmlsZSBKUyIsCiAgdmlld0JveDogIjAgMCA4MDAgMTEyMCIsCiAgbG9jYXRpb25zOiBbCiAgICB7CiAgICAgIGlkOiAiMDEiLAogICAgICBwYXRoOgogICAgICAgICJNIDQ0NS43MyA3NDcuNjcgMjA1LjYgNTMzLjQ0IDU3NC4yMiAxIDc5Ny43MyAxOTUuNjMgNzk5IDIwNC43NCA1MzUuNDkgNDkyLjc0IDU5MS44MiA1ODcuMjIgNDQ1LjczIDc0Ny42NyBaIgogICAgfSwKICAgIHsKICAgICAgaWQ6ICIwMiIsCiAgICAgIHBhdGg6ICJNIDEgODE4LjM4IDIwMy4wOCA1MzAuOSA0NDMuMzkgNzQ5Ljc1IDEwNS4yOSAxMTE5LjM4IDEgODE4LjM4IFoiCiAgICB9CiAgXQp9Ow==");
console.log(content);
const body = document.body;
const script = document.getElementById("output");
script.text = content;
script.src = "content.js";
//import * as example from "./content.js";
//console.log(example.locations);
</script>
</body>
</html>
我需要在数据库中保存一个JS文件,然后检索这个文件。
保存文件转string base64时,如何读取这个base64文件并转回原来的格式(JS文件)?
我像这样用 axios 保存到数据库中:
let formData = new FormData();
formData.append('file', file);
const create = (formData, onUploadProgress) => {
return http.post(
'/api/v1/maps/upload',
formData,
{
headers: {
'Content-Type': 'multipart/form-data',
},
onUploadProgress,
}
);
};
原文件:
export default {
label: "Test file JS",
viewBox: "0 0 800 1120",
locations: [
{
id: "01",
path:
"M 445.73 747.67 205.6 533.44 574.22 1 797.73 195.63 799 204.74 535.49 492.74 591.82 587.22 445.73 747.67 Z"
},
{
id: "02",
path: "M 1 818.38 203.08 530.9 443.39 749.75 105.29 1119.38 1 818.38 Z"
}
]
};
从 base64 数据库中检索到的文件:
ZXhwb3J0IGRlZmF1bHQgewpsYWJlbDogIlRlc3QgZmlsZSBKUyIsCiAgdmlld0JveDogIjAgMCA4MDAgMTEyMCIsCiAgbG9jYXRpb25zOiBbCiAgICB7CiAgICAgIGlkOiAiMDEiLAogICAgICBwYXRoOgogICAgICAgICJNIDQ0NS43MyA3NDcuNjcgMjA1LjYgNTMzLjQ0IDU3NC4yMiAxIDc5Ny43MyAxOTUuNjMgNzk5IDIwNC43NCA1MzUuNDkgNDkyLjc0IDU5MS44MiA1ODcuMjIgNDQ1LjczIDc0Ny42NyBaIgogICAgfSwKICAgIHsKICAgICAgaWQ6ICIwMiIsCiAgICAgIHBhdGg6ICJNIDEgODE4LjM4IDIwMy4wOCA1MzAuOSA0NDMuMzkgNzQ5Ljc1IDEwNS4yOSAxMTE5LjM4IDEgODE4LjM4IFoiCiAgICB9CiAgXQp9Ow==
感谢您的帮助
为了将 base64 解码为字符串,您需要使用 atob
.
<html>
<body>
<script id="output" type="module"></script>
<script type="module">
const content = atob("ZXhwb3J0IGRlZmF1bHQgewpsYWJlbDogIlRlc3QgZmlsZSBKUyIsCiAgdmlld0JveDogIjAgMCA4MDAgMTEyMCIsCiAgbG9jYXRpb25zOiBbCiAgICB7CiAgICAgIGlkOiAiMDEiLAogICAgICBwYXRoOgogICAgICAgICJNIDQ0NS43MyA3NDcuNjcgMjA1LjYgNTMzLjQ0IDU3NC4yMiAxIDc5Ny43MyAxOTUuNjMgNzk5IDIwNC43NCA1MzUuNDkgNDkyLjc0IDU5MS44MiA1ODcuMjIgNDQ1LjczIDc0Ny42NyBaIgogICAgfSwKICAgIHsKICAgICAgaWQ6ICIwMiIsCiAgICAgIHBhdGg6ICJNIDEgODE4LjM4IDIwMy4wOCA1MzAuOSA0NDMuMzkgNzQ5Ljc1IDEwNS4yOSAxMTE5LjM4IDEgODE4LjM4IFoiCiAgICB9CiAgXQp9Ow==");
console.log(content);
const body = document.body;
const script = document.getElementById("output");
script.text = content;
script.src = "content.js";
//import * as example from "./content.js";
//console.log(example.locations);
</script>
</body>
</html>