如何在Meteor(服务器端)中轻松创建文本文件并以.txt文件下载?
// On Server
if (Meteor.isServer) {
Meteor.methods({
'scan-db': function () {
// scan the DB for corrupted data
var allEntries = Jobs.find().fetch();
var report = "";
for ( let i = 0; i < allEntries.length ; i ++ ) {
if ( validate(allEntries[i]) == false ) {
report = report + allEntries[i].entryNumber + " has a problem" + "\n";
// used \n for line breaks in windows
}
return report; // a whole bunch of string
})
}
// On client
if (Meteor.isClient) {
Meteor.call("scan-db", function(err, res) {
if (res) {
downloadFile(res);
}
})
}
我希望能够将结果下载为文本文件进行保存。有没有简单的方法可以做到这一点?我尝试使用meteorhacks:picker
,但是尽管导入了Picker
undefined
返回了import { Picker } from 'meteor/meteorhacks:picker';
答案 0 :(得分:0)
如果要通过Meteor方法返回它,最简单的解决方案是返回文本内容,然后在客户端上生成文件。
您可以创建一个Blob,并使用诸如FileSaver之类的库来开始下载。如果您不想使用任何库,一个常见的技巧是生成指向blob文件的html链接,将其附加到DOM上并触发对其的点击。