f
.
save
(
io_file
)
data
=
io_file
.
getvalue
(
)
base64_data
=
base64
.
b64encode
(
data
)
base64_str
=
str
(
base64_data
,
'utf-8'
)
base64_data = base64_str.encode(encoding='utf-8')
data = base64.b64decode(base64_data)
<script>
function dataURLtoBlob(dataurl) {
var arr = dataurl.split(","),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
return new Blob([u8arr], { type: mime });
function downloadFile(url, name = "What's the fuvk") {
var a = document.createElement("a");
a.setAttribute("href", url);
a.setAttribute("download", name);
a.setAttribute("target", "_blank");
let clickEvent = document.createEvent("MouseEvents");
clickEvent.initEvent("click", true, true);
a.dispatchEvent(clickEvent);
function downloadFileByBase64(base64, fileName) {
var myBlob = dataURLtoBlob(base64);
var myUrl = URL.createObjectURL(myBlob);
downloadFile(myUrl, fileName);
</script>
注意的点:
bytesio转base64的适合记得要带上header,即mimetype类型
因为不同的文件类型base64前面拼接的不同
function getBase64Type(type) {
switch (type) {
case 'txt': return 'data:text/plain;base64,';
case 'doc': return 'data:application/msword;base64,';
case 'docx': return 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,';
case 'xls': return 'data:application/vnd.ms-excel;base64,';
case 'xlsx': return 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,';
case 'pdf': return 'data:application/pdf;base64,';
case 'pptx': return 'data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,';
case 'ppt': return 'data:application/vnd.ms-powerpoint;base64,';
case 'png': return 'data:image/png;base64,';
case 'jpg': return 'data:image/jpeg;base64,';
case 'gif': return 'data:image/gif;base64,';
case 'svg': return 'data:image/svg+xml;base64,';
case 'ico': return 'data:image/x-icon;base64,';
case 'bmp': return 'data:image/bmp;base64,';
function getType(file) {
var filename = file;
var index1 = filename.lastIndexOf(".");
var index2 = filename.length;
var type = filename.substring(index1 + 1, index2);
return type;
downloadExportFile(blob, fileName, fileType) {
const downloadElement = document.createElement('a')
let href = blob
if (typeof blob === 'string') {
downloadElement.target = '_blank'
} else {
href = window.URL.createObjectURL(blob)
downloadElement.href = href
downloadElement.download = fileName + '.' + fileType
document.body.appendChild(downloadElement)
downloadElement.click()
document.body.removeChild(downloadElement)
if (typeof blob !== 'string') {
window.URL.revokeObjectURL(href)
downloadFile(base64, fileName, fileType) {
const typeHeader = 'data:application/' + fileType + ';base64,'
const converedBase64 = typeHeader + base64
const blob = this.base64ToBlob(converedBase64, fileType)
this.downloadExportFile(blob, fileName, fileType)
this.downloadFile('你的base64数据','你的文件名称','你的文件数据类型');
上述方法有一定的问题,base64ToBlob方法是缺失的,可以参考方法1中的如何实现,主要干的事就是把base64的数据加上header,方法1的是在后端实现了,而该方法是在前端js中添加的,只不过方法并没有写,其实就是拼接。
functiondataURLtoBlob(dataurl){
vararr=dataurl.split(','),mime=arr[0].match(/:(.*?);/)[1],
bstr=atob(arr[1]),n=bstr.length,u8arr=newUint8Array(n);
while...
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>base64编码图片解析</title>
<script type="text/javascript">
functi...
let base64Str = "data:image/jpeg;base64,"+base64;
let aLink = document.createElement("a");
aLink.style.display = "none";
aLink.href = base64Str;
aLink.download = "test.jpg";
// 触发点击-然后移除
document.body.appendChild(aLink);
aLink.click();
document.body.remov.