簡體   English   中英

如何在Meteor中創建和下載文本文件?

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

如何在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';

如果要通過Meteor方法返回它,最簡單的解決方案是返回文本內容,然后在客戶端上生成文件。

您可以創建Blob並使用FileSaver等庫來開始下載。 如果您不想使用任何庫,常見的hack是生成blob文件的html鏈接,將其附加到DOM並觸發單擊它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM