1

我正在尝试在我的 web api 中读取图像流。

以下是我的代码。

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var Busboy = require('busboy');

app.use(bodyParser.json());

app.get('/', function(req, res) {
    res.send('Hello World!');
});

app.post('/', function(req, res){
var busboy = new Busboy({
        headers: request.headers
    });
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
        file.on('data', function(data) {
            console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
        });
        file.on('end', function() {
            console.log('File [' + fieldname + '] Finished');
        });
    });
});

app.listen(3000, function() {
    console.log('Example app listening on port 3000!');
});

当我使用 postman 测试上述 post 端点时,没有任何反应。似乎 busboy.on 从未被调用/触发。

Busboy npm https://www.npmjs.com/package/busboy#readme

以下是我的要求。通过提琴手追踪。

POST http://localhost:3000/ HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 879585
Postman-Token: 1defded8-01ff-eb6e-4afa-c49f63b477db
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarySrX3beIdojsVD2nB
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8,fr-CA;q=0.6,fr;q=0.4

------WebKitFormBoundarySrX3beIdojsVD2nB
Content-Disposition: form-data; name="File"; filename="Chrysanthemum.jpg"
Content-Type: image/jpeg
4

1 回答 1

1

似乎您已经在使用 busboy,但我仍然建议您尝试multer和 boundry&multipart 之类的东西不会打扰您。

但是,如果您想自己包装busboy,multer 中的这些代码也会很有用。

还记得检查一下。也许您在邮递员中覆盖了 Content-Type,这不是必需的。

于 2016-12-13T11:26:21.283 回答