Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I almost have this working. I use angular-file-upload (http://nervgh.github.io/pages/angular-file-upload/examples/image-preview/) to upload a file to nodejs server and I pipe it through to Azure.

router.post('/storeimg', function (req, res, next) {
    //create write stream for blob
    var blobSvc = azure.createBlobService();
    
    blobSvc.createContainerIfNotExists('images', function (error, result, response) {
        if (!error) {
            // Container exists and allows
            // anonymous read access to blob
            // content and metadata within this container

            var stream = blobSvc.createWriteStreamToBlockBlob(
                'images', 
                'test.png');
            
            //pipe req to Azure BLOB write stream
            req.pipe(stream);

            req.on('error', function (error) {
                //KO - handle piping errors
                console.log('error: ' + error);
            });
            req.once('end', function () {
                //OK
                console.log('all ok');
            });
        }
    });
});

The problem I have is that resulting file on Azure is prepended with the following

-----------------------------7df240321a0e9a
Content-Disposition: form-data; name="file"; filename="010218_osn.jpg"
Content-Type: image/jpeg

Which seems I also end up piping in the headers of the POST request. How do I avoid this? Is there a way to skip the headers?

UPDATE: Solution

var Busboy = require('busboy');

router.post('/storeimg', function (req, res, next) {
    //create write stream for blob
    var blobSvc = azure.createBlobService();
    
    blobSvc.createContainerIfNotExists('images', function (error, result, response) {
        if (!error) {
            // Container exists and allows
            // anonymous read access to blob
            // content and metadata within this container

            var busboy = new Busboy({ headers: req.headers });

            busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
                var stream = blobSvc.createWriteStreamToBlockBlob(
                    'images', 
                    filename);

                //pipe req to Azure BLOB write stream
                file.pipe(stream);
            });
            busboy.on('finish', function () {
                res.writeHead(200, { 'Connection': 'close' });
                res.end("That's all folks!");
            });

            req.pipe(busboy);

            req.on('error', function (error) {
                //KO - handle piping errors
                console.log('error: ' + error);
            });
            req.once('end', function () {
                //OK
                console.log('all ok');
            });
        }
    });
});

share|improve this question
1  
you probably shouln't pipe the request directly, mabe you can try to make a multipart reader from your request before – MayK Nov 20 '15 at 6:40
    
Thanks, MayK, you're entirely correct, of course. Posting update with problem solved via Busboy. – Igor Vaschuk Nov 20 '15 at 19:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.