javascript - 上传分块图像文件问题

标签 javascript angularjs node.js ng-file-upload http-chunked

我正在使用ng-file-upload指令从我的 Angular 应用程序将图像发送到服务器。这是我的代码:

Upload.upload({
    url: 'http://localhost:5000/upload',
    data: { file: blobFile },
    resumeChunkSize: 10000,
}).then(function (resp) { //upload function returns a promise
    console.log('resp: ', resp);
});

图像正在以 block 的形式传输。但现在,我在这里感到震惊。我不知道如何接收这些 block 并合并以创建完整图像。我的服务器代码如下:

handler: function (req, res) {
    var size = 0;

    req.on('data', function (data) {
      size += data.length;
      console.log('Got chunk: ' + data.length + ' total: ' + size);
    });

    req.on('end', function () {
      console.log("total size = " + size);
      res.send("response");
    });

    req.on('error', function (e) {
      console.log("ERROR ERROR: " + e.message);
    });
  }

每次,我收到一个 block 请求时,req.on('end', ...) 都会触发。我是一个新手,在这里很困惑。

最佳答案

是的..分块图像上传起来并不那么简单...

这是我使用的解决方案:

var request = require('request');
var fs = require('fs');

function uploadChunkedImage(url, callback){

    // request options
    var options = {  
        url: url, // the url of the image
        method: 'GET'
    };

    // array of chunks
    var chunks = [];

    // request
    request(options, function (err, res, body) {
        console.log('END')

        // body is corrupted, you can't use it for images... :-(
        // so, we will callback the concactened buffer of it instead

        // concact chunk buffers
        chunks = Buffer.concat(chunks); // image buffer that you can use

        // callback the result
        callback(err, res, chunks);

    }).on('response', function (response) {

        response.on('data', function (chunk) {
            // collect chunk buffer
            chunks.push(chunk);
        });

    });
}

uploadChunkedImage('http://localhost:5000/upload', function(err, res, buffer){
    if(!err){
         // save
         fs.writeFile('./myDirPath/image.jpg', buffer);
    }else{
         console.log(err);
    }

});

不要忘记使用 npm 在项目中安装 requestfsBuffer 是原生的

 npm install request --save
 npm install fs --save

了解更多信息:

你可以用 ng-file-upload 做同样的事情,有一个 good example right here ,否则我建议尝试如下(未测试):

handler: function (req, res) {

    // array of chunks
    var chunks= [];

    req.on('data', function (data) {

      // collect
      chunks.push(data);
      console.log('Got chunk: ' + data.length);

    });

    req.on('end', function () {

      // concact
      chunks = Buffer.concat(chunks);
      console.log("Total size = " + chunks.length);

      // res.send() is deprecated, use res.write instead
      res.write(chunks,'binary');
      res.end(null, 'binary');

    });

    req.on('error', function (e) {
      console.log("ERROR ERROR: " + e.message);
    });
}

希望能有所帮助

关于javascript - 上传分块图像文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46601737/

相关文章:

javascript - 寻找我的 jQuery 和 JS 脚本的帮助

javascript - 仅在 focusout 字段后调用 $scope.watch 函数

javascript - 有没有其他动态显示对话框的方法

javascript - 同时使用两个 Controller 的正确方法?

javascript - Node.js 使用 if 和异步调用控制流程

node.js - Grunt --force 失败, Node 为 : bad option

javascript - 将数据弹出窗口添加到 d3js map

javascript - Angular 限制对 View 的访问,除非有 cookie

jquery - UI-路由器 : How to preserve views while switching views

javascript - 异步回调内部调试