码迷,mamicode.com
首页 > Web开发 > 详细

js 转base64字符串为文件

时间:2020-03-27 00:33:26      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:fun   win   coder   nbsp   lob   cti   hoc   turn   bin   

   后台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字符串为文件

标签:fun   win   coder   nbsp   lob   cti   hoc   turn   bin   

原文地址:https://www.cnblogs.com/shihx/p/12578636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!