js 转base64字符串为文件
Posted 覃上
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 转base64字符串为文件相关的知识,希望对你有一定的参考价值。
后台java (jdk>=1.8):
byte[] bytes = byteArrayOutputStream.toByteArray();
String base64Str = java.util.Base64.getEncoder().encodeToString(bytes);
前端:
function dataURLtoBlob(base64Str) {
var bstr = atob(base64Str), n = bstr.length, u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: "application/vnd.ms-excel" }); //该类型为xls
}
var blob = dataURLtoBlob(base64Str);
var downloadUrl = window.URL.createObjectURL(blob);
var anchor = document.createElement("a");
anchor.href = downloadUrl;
anchor.download = "aa.xls";
anchor.click();
window.URL.revokeObjectURL(blob);
其他类型:
- //// ‘doc‘ => ‘application/msword‘,
- //// ‘bin‘ => ‘application/octet-stream‘,
- //// ‘exe‘ => ‘application/octet-stream‘,
- //// ‘so‘ => ‘application/octet-stream‘,
- //// ‘dll‘ => ‘application/octet-stream‘,
- //// ‘pdf‘ => ‘application/pdf‘,
- //// ‘ai‘ => ‘application/postscript‘,
- //// ‘xls‘ => ‘application/vnd.ms-excel‘,
- //// ‘ppt‘ => ‘application/vnd.ms-powerpoint‘,
- //// ‘dir‘ => ‘application/x-director‘,
- //// ‘js‘ => ‘application/x-javascript‘,
- //// ‘swf‘ => ‘application/x-shockwave-flash‘,
- //// ‘xhtml‘ => ‘application/xhtml+xml‘,
- //// ‘xht‘ => ‘application/xhtml+xml‘,
- //// ‘zip‘ => ‘application/zip‘,
- //// ‘mid‘ => ‘audio/midi‘,
- //// ‘midi‘ => ‘audio/midi‘,
- //// ‘mp3‘ => ‘audio/mpeg‘,
- //// ‘rm‘ => ‘audio/x-pn-realaudio‘,
- //// ‘rpm‘ => ‘audio/x-pn-realaudio-plugin‘,
- //// ‘wav‘ => ‘audio/x-wav‘,
- //// ‘bmp‘ => ‘image/bmp‘,
- //// ‘gif‘ => ‘image/gif‘,
- //// ‘jpeg‘ => ‘image/jpeg‘,
- //// ‘jpg‘ => ‘image/jpeg‘,
- //// ‘png‘ => ‘image/png‘,
- //// ‘css‘ => ‘text/css‘,
- //// ‘html‘ => ‘text/html‘,
- //// ‘htm‘ => ‘text/html‘,
- //// ‘txt‘ => ‘text/plain‘,
- //// ‘xsl‘ => ‘text/xml‘,
- //// ‘xml‘ => ‘text/xml‘,
- //// ‘mpeg‘ => ‘video/mpeg‘,
- //// ‘mpg‘ => ‘video/mpeg‘,
- //// ‘avi‘ => ‘video/x-msvideo‘,
-
//// ‘movie‘ => ‘video/x-sgi-movie‘,
以上是关于js 转base64字符串为文件的主要内容,如果未能解决你的问题,请参考以下文章