nodejs actionhero下载图片文件麻烦

时间:2014-04-28 17:37:37

标签: node.js actionhero

我正在制作简单的图片文件上传/下载服务器,并且我跟随此wiki page 。在这部分中,我传递了xpath参数,这是文件名,它对于文本文件都可以正常工作,但是当我为图像尝试相同时,我得到404.

  renderFile: function(api, connection, next){
    // do any pre-rendering etc here
    connection.rawConnection.responseHttpCode = 200;
    connection.sendFile(connection.params.xpath);
    next(connection, false);
  },

  run: function(api, connection, next){
    var self = this;
    if(connection.params.xpath == null){
      self.render404(api, connection, next);
    }else {
      var file = api.config.general.paths.public + '/' + connection.params.xpath
      console.log(file);
      fs.exists(file, function(found){
        if(found){
          self.renderFile(api, connection, next);
        }else {
          self.render404(api, connection, next);
        }
      });
    }
  }

我错过了任何设置或配置吗?

1 个答案:

答案 0 :(得分:1)

对我来说一切似乎都没问题。在空白的actionhero项目中,我可以使用下面的操作,http://localhost:8080/api/file?xpath=logo/actionhero.png之类的请求按预期工作

var fs = require('fs');

exports.action = {
  name:                   'file',
  description:            'file',
  blockedConnectionTypes: [],
  outputExample:          {},
  matchExtensionMimeType: false,
  version:                1.0,
  toDocument:             true,

  inputs: {
    required: [],
    optional: ['xpath'],
  },

  render404: function(api, connection, next){
    connection.rawConnection.responseHttpCode = 404;
    connection.sendFile('404.html');
    next(connection, false);
  },

  renderFile: function(api, connection, next){
    connection.rawConnection.responseHttpCode = 200;
    connection.sendFile(connection.params.xpath);
    next(connection, false);
  },

  run: function(api, connection, next){
    var self = this;
    if(connection.params.xpath == null){
      self.render404(api, connection, next);
    }else {
      fs.exists(file, function(found){
        if(found){
          self.renderFile(api, connection, next);
        }else {
          self.render404(api, connection, next);
        }
      });
    }
  }
};