Node. js: How does the server obtain the File Upload progress and node. js file upload?

Source: Internet
Author: User

Node. js: How does the server obtain the File Upload progress and node. js file upload?

Overview

multerIs a commonly used Express File Upload middleware. How the server obtains the File Upload progress is a common problem during use. On SF, some people also asked a similar question: Does nodejs multer check the file upload progress? I answered a few questions and sorted them out here. If you have the same questions, you can refer to them.

The following describes how to useprogress-streamObtain the File Upload progress and the precautions for using this component.

Use progress-stream to get the file upload progress

To obtain the upload progress on the server, try the following code. Note that this module is not strongly bound to Express and multer and can be used independently.

Var fs = require ('fs'); var express = require ('express '); var multer = require ('multer '); var progressStream = require ('ss SS-stream'); var app = express (); var upload = multer ({dest: 'upload/'}); app. post ('/upload', function (req, res, next) {// create a progress stream instance var progress = progressStream ({length: '0 '}); // note that the length is set to '0' req. pipe (progress); progress. headers = req. headers; // obtain the actual length of the uploaded file (for multipart) progress. on ('length', function nowIKnowMyLength (actualLength) {console. log ('actuallength: % s', actualLength); progress. setLength (actualLength) ;}); // gets the upload progress SS. on ('progress', function (obj) {console. log ('Progress: % s', obj. percentage) ;}); // upload File upload. single ('logo ') (progress, res, next) ;}); app. post ('/upload', function (req, res, next) {res. send ({ret_code: '0'}) ;}); app. get ('/Form', function (req, res, next) {var form = fs. readFileSync ('. /form.html ', {encoding: 'utf8'}); res. send (form) ;}); app. listen (0, 3000 );

How to obtain the actual size of the uploaded file

Multipart type. You must listen to the length to obtain the actual file size. (In the official document, the conviction event is actually a problem)

// Obtain the actual length of the uploaded file (for multipart) progress. on ('length', function nowIKnowMyLength (actualLength) {console. log ('actuallength: % s', actualLength); progress. setLength (actualLength );});

3. Is there a bug in progress-stream getting the actual file size?

For multipart upload, when the progress-stream instance is initialized, the parameter length needs to be passed to a non-numeric type. Otherwise, the obtained progress will always be 0, and then jump directly to 100.

As for why, it should be a bug in the SS-steram module. Check the source code of the module. When length is of the number type, the code is skipped directly. Therefore, length is always regarded as 0.

tr.on('pipe', function(stream) {  if (typeof length === 'number') return;  // Support http module  if (stream.readable && !stream.writable && stream.headers) {    return onlength(parseInt(stream.headers['content-length'] || 0));  }  // Support streams with a length property  if (typeof stream.length === 'number') {    return onlength(stream.length);  }  // Support request module  stream.on('response', function(res) {    if (!res || !res.headers) return;    if (res.headers['content-encoding'] === 'gzip') return;    if (res.headers['content-length']) {      return onlength(parseInt(res.headers['content-length']));    }  });});

Summary

The above is a small part of the Node. the js file upload function allows the server to obtain the File Upload progress. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.