简体   繁体   English

如何在Meteor中创建和下载文本文件?

[英]How to create and download a text file in Meteor?

How can I create a text file easily in Meteor (Server Side) and download as a .txt file? 如何在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);
     }
  })
}

I am hoping to be able to download my result as a text file to save. 我希望能够将我的结果下载为文本文件以进行保存。 Is there any easy way of doing this? 这有什么简单的方法吗? I tried using meteorhacks:picker but apparently the package is not working or Picker returned undefined despite importing it import { Picker } from 'meteor/meteorhacks:picker'; 我尝试使用meteorhacks:picker但显然包没有工作或Picker返回undefined尽管import { Picker } from 'meteor/meteorhacks:picker';

If you want to return it through a Meteor method, the easiest solution is to return the text content and then generate the file on the client. 如果要通过Meteor方法返回它,最简单的解决方案是返回文本内容,然后在客户端上生成文件。

You can create a Blob and use a library such as FileSaver to start the download. 您可以创建Blob并使用FileSaver等库来开始下载。 If you don't want to use any library, a common hack is to generate an html link to the blob file, append it to the DOM and trigger a click on it. 如果您不想使用任何库,常见的hack是生成blob文件的html链接,将其附加到DOM并触发单击它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM