1

我正在尝试使用NodeJS busboy在服务器上上传图像文件,但出现此错误:

Service Listening for request on: 8080
Error: Unsupported content type: image/png
    at Busboy.parseHeaders (D:\ImageUploadService\node_modules\busboy\lib\main.j
s:66:9)
    at new Busboy (D:\ImageUploadService\node_modules\busboy\lib\main.js:21:10)
    at D:\ImageUploadService\server.js:15:15
    at Layer.handle [as handle_request] (D:\ImageUploadService\node_modules\expr
ess\lib\router\layer.js:76:5)
    at next (D:\ImageUploadService\node_modules\express\lib\router\route.js:100:
13)
    at Route.dispatch (D:\ImageUploadService\node_modules\express\lib\router\rou
te.js:81:3)
    at Layer.handle [as handle_request] (D:\ImageUploadService\node_modules\expr
ess\lib\router\layer.js:76:5)
    at D:\ImageUploadService\node_modules\express\lib\router\index.js:234:24
    at Function.proto.process_params (D:\ImageUploadService\node_modules\express
\lib\router\index.js:312:12)
    at D:\ImageUploadService\node_modules\express\lib\router\index.js:228:12

下面是我的代码:

app.post('/uploadImage',function(req,res){
    var alias=req.query.alias;
    var imagetype=req.query.imagetype;  //can be media/profile
    var busboy = new Busboy({ headers: req.headers });
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        var saveTo = ".\\Images\\"+alias+"\\"+imagetype+"\\"+filename;
        if (fs.existsSync(saveTo)) {
            file.pipe(fs.createWriteStream(saveTo));
        }
        else{
            fs.mkdir(".\\Images\\"+alias+"\\"+imagetype,function(err){
                 saveTo=".\\Images\\"+alias+"\\"+imagetype;
                 file.pipe(fs.createWriteStream(saveTo));
            });
        }   
    });
    busboy.on('finish', function() {
      res.writeHead(200, { 'Connection': 'close' });
      res.status(200).end();
    });
    return req.pipe(busboy);
});

我正在尝试使用POSTMAN REST CLIENT发出请求。

这是客户的问题还是我必须在代码中进行更改。请注意,我Content-Type: image/png在客户端(邮递员)上提到过。

附带问题:另外,我有一种方法可以将图像存储为拇指?

4

1 回答 1

6

Busboy 只解析application/x-www-form-urlencodedmultipart/form-data请求。如果您要发送原始文件数据,则必须手动处理(这很简单,因为不涉及解析)。

于 2014-09-30T18:07:17.297 回答