Koa.js应用程序将变量中的数据下载为文件

时间:2018-08-08 09:56:56

标签: javascript node.js koa koa2

在我的koa.js应用程序中,我正在调用一个从数据库获取数据的函数。该路线如下所示:

class MyMailer < Devise::Mailer   
  helper :application # gives access to all helpers defined within `application_helper`.
  include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
  default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views
  include DefaultUrlOptions
end

现在,我想将此数据(在router.post("/data", async ctx => { const info = await controller.getData( ctx.request.body.from, ctx.request.body.to ); ctx.body = info; /* *** EXPECTING SOMETHING LIKE THIS TO INVOKE DOWNLOAD OF THE BROWSER *** let fname = "info_" + String(Number(new Date())) + ".txt"; ctx.set('Content-disposition', `attachment; filename=${fname}`); ctx.statusCode = 200; ctx.body = fs.createReadStream(info); */ }); JSON变量中)作为文件发送到客户端(Web浏览器),以便浏览器下载它。

0 个答案:

没有答案