zoukankan      html  css  js  c++  java
  • 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', 
  • 相关阅读:
    AI
    CentoOS6.6安装netcat
    ip防刷脚本
    php git pull
    冥想_ PHP抽奖程序概率算法
    如何在CentOS配置Apache的HTTPS服务
    C++ 用RGB 三种颜色绘图
    Linux Vsftpd 连接超时解决方法(被动模式)
    js 函数返回函数
    模拟jquery的$()选择器的实现
  • 原文地址:https://www.cnblogs.com/shihx/p/12578636.html
Copyright © 2011-2022 走看看