search

Home  >  Q&A  >  body text

node.js - Nodejs uploads large files (videos) using [resumable.js + formidable]

1. Requirements: Upload a large file [video] and process it with Nodejs;
2. The front-end uses resumable to slice and upload the video, and the back-end uses formidable to use Buffer deal with

Problems encountered [Use a text as an example to upload]:
1. 后端写入成功后,无法打开文件【自动添加POST中相关的值】
   

X.Code part

1.app.js

app.post('/upload', function(req, res){
      var form = new formidable.IncomingForm();
      //后缀名
      form.keepExtensions = true;
      //保存路径
      form.uploadDir = './';
      form.parse(req, function(err, fields, files) {
          //console.log(util.inspect({fields: fields, files: files}));
      });


      var dd=[],ll=0;
      form.handlePart=function(part) {
        var dd=[],ll=0;
        //Buffer加入数组
        part.on('data', function(data) {
          if (data.length == 0) {
            return;
          }
          dd.push(data);
          ll+=data.length;
        });

        part.on('end', function() {
        //获取文件名
          var p = './' + req.query.resumableFilename;
            fs.open(p, 'a', function (err, fd) {
              if (err) {
                throw err;
              }
          var writeBuffer = dd,
              offset = 0,
              len = writeBuffer.length,
              filePostion = null;
              //写人Buffer
              fs.write(fd, Buffer.concat(dd, ll), 0, ll, filePostion, function(){

              })
            });
        });
      }
});
2.Charles packet capture data
POST /upload?resumableChunkNumber=1&resumableChunkSize=1048576&resumableCurrentChunkSize=393&resumableTotalSize=393&resumableType=text%2Frtf&resumableIdentifier=393-rtf&resumableFilename=%E6%96%87%E6%9C%AC.rtf&resumableRelativePath=%E6%96%87%E6%9C%AC.rtf&resumableTotalChunks=1 HTTP/1.1
Host: 127.0.0.1:4000
------WebKitFormBoundaryeLW6trgJbBaZlvgP
Content-Disposition: form-data; name="file"; filename="文本.rtf"
Content-Type: application/octet-stream

------WebKitFormBoundaryeLW6trgJbBaZlvgP
Content-Disposition: form-data; name="resumableChunkNumber"

1
------WebKitFormBoundaryeLW6trgJbBaZlvgP
Content-Disposition: form-data; name="resumableChunkSize"

1048576
------WebKitFormBoundaryeLW6trgJbBaZlvgP
Content-Disposition: form-data; name="resumableCurrentChunkSize"

393
------WebKitFormBoundaryeLW6trgJbBaZlvgP
Content-Disposition: form-data; name="resumableTotalSize"

393
------WebKitFormBoundaryeLW6trgJbBaZlvgP
Content-Disposition: form-data; name="resumableType"

text/rtf
------WebKitFormBoundaryeLW6trgJbBaZlvgP
Content-Disposition: form-data; name="resumableIdentifier"

393-rtf
------WebKitFormBoundaryeLW6trgJbBaZlvgP
Content-Disposition: form-data; name="resumableFilename"

文本.rtf
------WebKitFormBoundaryeLW6trgJbBaZlvgP
Content-Disposition: form-data; name="resumableRelativePath"

文本.rtf
------WebKitFormBoundaryeLW6trgJbBaZlvgP
Content-Disposition: form-data; name="resumableTotalChunks"

1
------WebKitFormBoundaryeLW6trgJbBaZlvgP
Content-Disposition: form-data; name="file"; filename="文本.rtf"
Content-Type: application/octet-stream
{\rtf1\ansi\ansicpg936\cocoartf1504
{\fonttbl\f0\fnil\fcharset134 PingFangSC-Regular;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;\csgray\c100000;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0

\f0\fs24 \cf0 \'c4\'e3\'ba\'c3}
------WebKitFormBoundaryeLW6trgJbBaZlvgP--
3. Data obtained by the server

Write the content of the file

11048576393393text/rtf393-rtf文本.rtf文本.rtf1{\rtf1\ansi\ansicpg936\cocoartf1504
{\fonttbl\f0\fnil\fcharset134 PingFangSC-Regular;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;\csgray\c100000;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0

\f0\fs24 \cf0 \'c4\'e3\'ba\'c3}
4.The problem

The first part of the file saved on the server side has more data than the data submitted by the front end. The following content`Please enter the code

11048576393393text/rtf393-rtf文本.rtf文本.rtf1

This content is the splicing

of the values ​​corresponding to the URL parameters in POST, which is the judgment and segmentation of the boundary in the http protocol.
If you manually delete this part of the characters, the file can be opened

problem lies in

1. How to prevent it from automatically adding this attribute?
2. Is it possible to follow this writing method when uploading large videos?

If you have any ideas, please give me some advice, thank you very much

过去多啦不再A梦过去多啦不再A梦2745 days ago1465

reply all(1)I'll reply

  • 黄舟

    黄舟2017-05-19 10:50:07

    There is an article that is more suitable for some of your problems. You need to read it: Pure js implements file slicing upload, breakpoint resume upload, the code is simple and clear
    /a/11...

    reply
    0
  • Cancelreply