简体   繁体   English

POST 请求的进度跟踪器

[英]Progress tracker for POST request

Hello I am fairly new to NodeJS and its modules but I was wondering if I can get some help with this, I am trying to create a progress bar for an application but it only returns 100% when it completes,I don't think i'm using piping right either but any help would be greatly appreciated!您好,我对 NodeJS 及其模块还很陌生,但我想知道是否可以得到一些帮助,我正在尝试为应用程序创建一个进度条,但它在完成时只返回 100%,我不认为我'正在使用管道,但任何帮助将不胜感激!

I am also transmitting data through a socket as well if that makes a difference.如果这有所作为,我也会通过套接字传输数据。

const request = require('request');
const io = require('socket.io')(http)
const fs = require('fs');
var progress = require('progress-stream');

var str = progress({
    time: 1000
});

str.on('progress', function (progress) {
    console.log(Math.round(progress.percentage) + '%');
});


io.on('connection', (socket)=>{
socket.on('fileUploader',()=>{
    let formData = {
            preview_file: fs.createReadStream(filePath};
        request.post({
                url: 'http://httpbin.org/post',
                formData: formData
            }, 
                function optionalCallback(err, httpResponse, body) {
                if (err) {
                    return console.error('upload failed:', err);
                }
                console.log('Upload successful!  Server responded with:', body);
        }).pipe(str);
    })
})

Try using axios , they have onUploadProgress method, which is you can use the "progressEvent" callback from it to get percentage.尝试使用axios ,他们有 onUploadProgress 方法,你可以使用它的“progressEvent”回调来获取百分比。

The documentation from axios is pretty clear about this . axios 的文档对此非常清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM