1

I am trying to upload a image file on server using NodeJS busboy and I am getting this error:

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

Below is my code:

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);
});

I am trying to make request using POSTMAN REST CLIENT.

Is this a problem from client or do I have to make chnages in the code. Please note that I've mentioned Content-Type: image/png on the client(postman).

Side question: Also, I there a way I can store the image as thumbs ??

writeToBhuwan
  • 3,233
  • 11
  • 39
  • 67

1 Answers1

6

Busboy only parses application/x-www-form-urlencoded and multipart/form-data requests. If you're sending raw file data, you have to handle that manually (and that's trivial because there is no parsing involved).

mscdex
  • 104,356
  • 15
  • 192
  • 153