2

我几乎有这个工作。我使用 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');
            });
        }
    });
});

4

0 回答 0