0

我正在构建简单的图像文件上传/下载服务器,并且正在关注这个wiki 页面 。在这一部分中,我传递了作为文件名的 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);
        }
      });
    }
  }

有没有我错过的设置或配置?

4

1 回答 1

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);
        }
      });
    }
  }
};
于 2014-04-29T18:14:13.817 回答