1

我创建了一个 node.js 服务器,它使用 busboy 来接受请求,并将文件通过管道传输到 Imgur 进行上传。但是,我不断收到“上传文件太快!” 来自 Imgur 的回应,我不确定到底是什么问题。这是涉及 busboy 的代码片段:

var express = require('express');
var Busboy = require('busboy');
var fs = require('fs');
var request = require('request-promise');
var router = express.Router();

router.post('/u', function(req, res, next) {
    var busboy = new Busboy({headers: req.headers});
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        if(fieldname == 'image') {
            var options = {
                uri: 'https://api.imgur.com/3/image',
                method: 'POST',
                headers: {
                    'Authorization': 'Client-ID ' + clientID // put client id here
                },

                form: {
                    image: file,
                    type: 'file'
                }
            };

            request(options)
                .then(function(parsedBody) {
                    console.log(parsedBody);
                })
                .catch(function(err) {
                    console.log(err);
                });
        }
    });
    busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
        console.log('field');
    });
    busboy.on('finish', function() {
        res.status(200).end();
    });
    req.pipe(busboy);
});

如您所见,我将请求文件直接传送到我对 imgur 的请求中。通过简单地将文件保存到磁盘然后使用fs.createReadStream()完美地提供 ReadStream,所以我不确定为什么尝试直接从请求到请求的管道会给我错误。我从 Imgur 得到的确切回复是:

StatusCodeError: 400 - {"data":{"error":"Uploading file too fast!","request":"\/3\/image","method":"POST"},"success":false,"status":400} 

如果有人以前遇到过这种情况,那将很有帮助...

4

1 回答 1

3

第一个问题是您应该使用 formData 而不是 form 进行文件上传。否则,请求库将不会发送正确的 HTTP 请求。

第二个问题是流对象在完全处理之前不会具有正确的内容长度。我们可以自己缓冲数据,并在处理来自 busboy 的初始文件流后传递它。*

这给了我们一些看起来像

var express = require('express');
var Busboy = require('busboy');
var fs = require('fs');
var request = require('request-promise');
var router = express.Router();

router.post('/u', function(req, res, next) {
    var busboy = new Busboy({headers: req.headers});
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        if(fieldname == 'image') {
            // the buffer
            file.fileRead = [];
            file.on('data', function(data) {
                // add to the buffer as data comes in
                this.fileRead.push(data);
            });

            file.on('end', function() {
                // create a new stream with our buffered data
                var finalBuffer = Buffer.concat(this.fileRead);

                var options = {
                    uri: 'https://api.imgur.com/3/image',
                    method: 'POST',
                    headers: {
                        'Authorization': 'Client-ID ' + clientID // put client id here
                    },

                    formData: {
                        image: finalBuffer,
                        type: 'file'
                    }
                };

                request(options)
                    .then(function(parsedBody) {
                        console.log(parsedBody);
                    })
                    .catch(function(err) {
                        console.log(err);
                    });
            });
        }
    });
    busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
        console.log('field');
    });
    busboy.on('finish', function() {
        res.status(200).end();
    });
    req.pipe(busboy);
});

最后,您可能需要考虑使用 request 库,因为 request-promise 库不鼓励使用流。有关更多详细信息,请参阅 github 存储库:https ://github.com/request/request-promise

于 2016-02-21T20:17:02.537 回答