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);

其他类型:
  1.  //// ‘doc‘ => ‘application/msword‘,
  2.  //// ‘bin‘ => ‘application/octet-stream‘,
  3.  //// ‘exe‘ => ‘application/octet-stream‘,
  4.  //// ‘so‘ => ‘application/octet-stream‘,
  5.  //// ‘dll‘ => ‘application/octet-stream‘,
  6.  //// ‘pdf‘ => ‘application/pdf‘,
  7.  //// ‘ai‘ => ‘application/postscript‘,
  8.  //// ‘xls‘ => ‘application/vnd.ms-excel‘,
  9.  //// ‘ppt‘ => ‘application/vnd.ms-powerpoint‘,
  10.  //// ‘dir‘ => ‘application/x-director‘,
  11.  //// ‘js‘ => ‘application/x-javascript‘,
  12.  //// ‘swf‘ => ‘application/x-shockwave-flash‘,
  13.  //// ‘xhtml‘ => ‘application/xhtml+xml‘,
  14.  //// ‘xht‘ => ‘application/xhtml+xml‘,
  15.  //// ‘zip‘ => ‘application/zip‘,
  16.  //// ‘mid‘ => ‘audio/midi‘,
  17.  //// ‘midi‘ => ‘audio/midi‘,
  18.  //// ‘mp3‘ => ‘audio/mpeg‘,
  19.  //// ‘rm‘ => ‘audio/x-pn-realaudio‘,
  20.  //// ‘rpm‘ => ‘audio/x-pn-realaudio-plugin‘,
  21.  //// ‘wav‘ => ‘audio/x-wav‘,
  22.  //// ‘bmp‘ => ‘image/bmp‘,
  23.  //// ‘gif‘ => ‘image/gif‘,
  24.  //// ‘jpeg‘ => ‘image/jpeg‘,
  25.  //// ‘jpg‘ => ‘image/jpeg‘,
  26.  //// ‘png‘ => ‘image/png‘,
  27.  //// ‘css‘ => ‘text/css‘,
  28.  //// ‘html‘ => ‘text/html‘,
  29.  //// ‘htm‘ => ‘text/html‘,
  30.  //// ‘txt‘ => ‘text/plain‘,
  31.  //// ‘xsl‘ => ‘text/xml‘,
  32.  //// ‘xml‘ => ‘text/xml‘,
  33.  //// ‘mpeg‘ => ‘video/mpeg‘,
  34.  //// ‘mpg‘ => ‘video/mpeg‘,
  35.  //// ‘avi‘ => ‘video/x-msvideo‘,
  36. //// ‘movie‘ => ‘video/x-sgi-movie‘, 

以上是关于js 转base64字符串为文件的主要内容,如果未能解决你的问题,请参考以下文章

JS 图片转Base64

js中字符串转base64和base64转字符串

JS 图片转Base64

将 base64 图像字符串转换为可以使用 node.js 提供给浏览器的图像文件

mui开发app之js将base64转图片文件

js base64转二进制