【问题标题】:Mocha/Superagent: Unable to upload fileMocha/Superagent:无法上传文件
【发布时间】:2018-08-03 17:09:51
【问题描述】:

我正在尝试编写测试以使用 Mocha/Superagent 上传文件。当此 API 被客户端访问时,它可以完美运行。当我运行测试时,它以 500 响应代码失败。我只是不知道是什么问题。这是我正在使用的代码。

var request = require('supertest')("http://localhost:8081");
var app = require('../app.js');
var chai = require('chai');
var expect = chai.expect;
var should = chai.should();
var filePath = '/test/myFile.jpg';//Folder where the file is located

var customHeaders = {
    "authToken": "Whatever"
};

describe('POST /files/upload/', function() {    
    it('should upload a file', function(done) {
        request
            .post('/files/upload/')
            .set(customHeaders)
            .field('UserName', 'ABC')
            .attach('image', filePath)
            .expect(200)
            .end(function(err, res) {
                console.log(res);
                console.log(err);
                expect(res.body.fileId).to.be.above(0);
                done();
            });
    });
});

有人有使用 Mocha 和 Superagent 上传文件的工作示例吗?

【问题讨论】:

    标签: node.js mocha.js superagent


    【解决方案1】:

    我也有一个和你很相似的问题。让我解释一下。

    这部分filePath可能是问题所在。

    var filePath = 'path/to/file.png'

    我改成之后连运气都没有

    var filePath = require('path/to/file.png')

    但是,通过尝试 require(path),它让我了解到,我可以将附件放在下面。

    .attach('htmlInputTagName', '/home/hary/Documents/web-dashboard/public/img/logo.png')

    让我们看看你是否在服务器端的req.filesreq.body 中得到了什么。

    编辑: 我刚刚意识到我的解决方案专用于本地服务器。如果你想在其他服务器上运行它,我的解决方案是无效的。

    【讨论】:

      猜你喜欢
      • 2018-02-28
      • 2014-02-23
      • 1970-01-01
      • 2015-10-10
      • 2016-05-15
      • 2016-03-27
      • 1970-01-01
      • 2016-07-25
      相关资源
      最近更新 更多