有一个需求是需要下载页面上的所有链接导出并通过浏览器下载到本地
使用了3个js库
| 1 2 3 | <script src="https://cdn.jsdelivr.net/npm/jszip@3.10.0/dist/jszip.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/file-saver@2.0.2/dist/FileSaver.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script> | 
准备内容
提前将内容都准备好放入magnet,这里使用了$loop->last来规避最后单独出现连接字符
| 1 | var magnet = @foreach ($data as $list) @if ($loop->last) "{{ $list->dl_url2  }}\n" @else "{{ $list->dl_url2  }}\n" + @endif @endforeach; | 
下载txt方法
这里使用了FileSaver.min.js,cdn直接引入后不需要require,直接用jquery绑定dom调用saveAs即可
| 1 2 | var blob = new Blob([magnet], {type: "text/plain;charset=utf-8"}); jQuery("#blob2").on("click", function () {saveAs(blob, "hello world.txt");}); | 
下载zip方法
打包这里使用了jszip.min.js,可以打包为zip,用jquery绑定dom调用
| 1 2 3 4 5 6 7 8 9 | var zip = new JSZip(); zip.file("torrent.txt", magnet); jQuery("#blob").on("click", function () {     zip.generateAsync({type:"blob"}).then(function (blob) { // 1) generate the zip file         saveAs(blob, "torrent.zip");                          // 2) trigger the download     }, function (err) {         jQuery("#blob").text(err);     }); }); | 
There are no comments yet