首页 > 解决方案 > 通过 RestBulider 发送和接收文件

问题描述

controller on localhost:8000
const fs = require("fs");
exports.install = function () {
  ROUTE("GET     /", indexPage);
};

function indexPage() {
  var self = this;

  console.log(" In GET ROUTE");


  RESTBuilder.GET("http://127.0.0.1:8500/getFile/").stream(function (
    err,
    response
  ) {
    if (err) {
      console.log(err);
      return;
    }
   
    var writer = fs.createWriteStream("./public/testBuilder.txt");
    // console.log("Writing to file");
    response.pipe(writer);
    self.json({ thankyou: "ok" });
  });
}


controller on localhost:8500

exports.install = function () {
  ROUTE("GET /getFile/", test);
};

function test() {
  var self = this;
  console.log("#################");
  console.log(self.body); 
  self.file("~trimSail/restBuilder.txt");
  // });
}

上面的代码在 totaljs 3 中有效,但在 total4 中失败。发送文件以响应 RestBuilder.GET 并将响应流式传输到文件。错误 response.pipe 不是函数。

标签: total.js

解决方案


首先,请清理您的代码。

针对 Total.js 进行了优化

const Fs = require('fs');

exports.install = function () {
    ROUTE('GET /', index);
};

function index() {

    var $ = this;

    console.log('In GET ROUTE');

    RESTBuilder.GET('http://127.0.0.1:8500/getFile/').stream($.successful(function(response) { 
        var writer = Fs.createWriteStream('./public/testBuilder.txt');
        response.stream.pipe(writer);
        $.json({ thankyou: 'ok' });
    }));

}

推荐阅读