简体   繁体   English

nodejs actionhero下载图片文件的麻烦

[英]nodejs actionhero download picture file trouble

I'm building simple image file upload/download server and I'm following this wiki page . 我正在构建简单的图像文件上传/下载服务器,并且正在关注此Wiki页面 In this part I'm passing the xpath parameter which is the file name and it all works fine for text files but I'm getting 404 when trying the same for images. 在这一部分中,我传递了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);
        }
      });
    }
  }

is there any settings or config I missed? 我错过了任何设置或配置吗?

Everything seems OK to me. 一切对我来说似乎还可以。 In a blank actionhero project, I can use the action below, and requests like http://localhost:8080/api/file?xpath=logo/actionhero.png work as expected 在一个空白的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