js文件流,导出txt

js文件流,导出txt


var content = "file content!";
var data = new Blob([content],{type:"text/plain;charset=UTF-8"});
var downloadUrl = window.URL.createObjectURL(data);
var anchor = document.createElement("a");
anchor.href = downloadUrl;
anchor.download = "文件名.txt";
anchor.click();
window.URL.revokeObjectURL(data);

//文件下载
var blob = new Blob([要保存的文件流], { type: 'application/octet-stream' }),
fileName = 'filename' + path.substring(path.lastIndexOf("."), path.length);
////filename,摘取了常用的部分,其实还有其他一些mimetypes = array(
////    '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',
////);
downFile(blob, fileName);

//js下载文件流
    function downFile(blob, fileName) {
        if (window.navigator.msSaveOrOpenBlob) {
            navigator.msSaveBlob(blob, fileName);
        } else {
            var link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            link.download = fileName;
            link.click();
            window.URL.revokeObjectURL(link.href);
        }
    }

转自:https://blog.csdn.net/lixiaoer757/article/details/80272281

你可能感兴趣的:(文件流)