我正在尝试使用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发出请求。
这是来自客户端的问题还是我必须在代码中创建chnages。请注意,我在客户(邮递员)上提到过Content-Type: image/png
。
旁边问题:另外,我有办法将图像存储为拇指吗?
答案 0 :(得分:4)
Busboy只解析application/x-www-form-urlencoded
和multipart/form-data
个请求。如果你要发送原始文件数据,你必须手动处理(这是微不足道的,因为没有涉及解析)。