title: 前端如何进行压缩下载 date: 2017-04-08 11:40:49 tags: categories: plugin
需要使用 jszip和FileSaver这两个插件
mark
mark
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="button" onclick="create_zip()" value="创建压缩包并下载"/>
</body>
</html>
<script src="./node_modules/jszip/dist/jszip.js"></script>
<script src="./node_modules/file-saver/FileSaver.js"></script>
<script>
function create_zip() {
var zip = new JSZip();
zip.file("hello1.txt", "Hello majunchang World\n");
zip.file("hello2.txt", "Hello liuyifei World\n");
zip.generateAsync({type:"blob"})
.then(function(content) {
saveAs(content, "liuyifei.zip");
});
}
</script>