繁体   English   中英

nodejs actionhero下载图片文件的麻烦

[英]nodejs actionhero download picture file trouble

我正在构建简单的图像文件上传/下载服务器,并且正在关注此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);
        }
      });
    }
  }

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

一切对我来说似乎还可以。 在一个空白的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);
        }
      });
    }
  }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM