将带有图像的angular-file-upload请求传递给node.js中的Azure

时间:2015-11-20 04:31:44

标签: javascript angularjs node.js angular-file-upload azure-blob-storage

我几乎有这个工作。我使用angular-file-upload(http://nervgh.github.io/pages/angular-file-upload/examples/image-preview/)将文件上传到nodejs服务器,然后将其传输到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');
            });
        }
    });
});




我遇到的问题是Azure上的结果文件前面加上以下

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

看来我最终还是在POST请求的标题中输入了管道。我该如何避免这种情况?有没有办法跳过标题?

更新:解决方案



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




0 个答案:

没有答案