简体   繁体   English

超级代理文件上传的进度条

[英]Progress bar for superagent file upload

I am using the following codes to upload files.我正在使用以下代码上传文件。 File would be sent through superagent in blob format, convert it to dataURI when reaches server and save into AWS S3.文件将以 blob 格式通过超级代理发送,到达服务器时将其转换为 dataURI 并保存到 AWS S3 中。

files.forEach((file) => {
  superagent.post('http://localhost:3030/uploads')
  .attach('file', file)
  .on('progress', e => {
    console.log('Percentage done: ', e);
  })
  .end((err, res) => {
    // console.log(err);
    console.log(res);
  });
});

File upload works but the progress bar is not populated correctly.文件上传有效,但进度条未正确填充。

1

As per the screenshot, you can see that the ProgressEvent upload percentage ended very fast at 100%, due to both frontend and backend hosted in a same server.根据屏幕截图,您可以看到 ProgressEvent 上传百分比以 100% 的速度结束,因为前端和后端都托管在同一服务器中。

The upload progress continued in the background but the direction became "download" instead of "upload" from 14:14:08 to 14:14:14 and completed with a response.上传进度在后台继续进行,但方向从 14:14:08 到 14:14:14 变为“下载”而不是“上传”,并以响应完成。

I could not calculate the progress based on "total - loaded" too, because during the "download" progress, the total is 0.我也无法根据“总加载”计算进度,因为在“下载”进度期间,总数为 0。

2

Tried replacing superagent with axios and hit the same problem.尝试用 axios 替换 superagent 并遇到同样的问题。

files.forEach((file) => {
  const url = 'http://localhost:3030/uploads';
  const formData = new FormData();
  const config = {
    headers: {
      'content-type': 'multipart/form-data'
    },
    onUploadProgress: e => {
      console.log('Upload: ', e);
    },
    onDownloadProgress: e => {
      console.log('Download: ', e);
    }
  };
  formData.append('file', file);
  axios.post(url, formData, config)
    .then(res => console.log(res))
    .catch(err => console.log(err));
});

How could I build a progress bar out of it?我怎么能用它建立一个进度条? Can I conclude that the "upload" is the process of uploading to the server, while "download" is the process of uploading to AWS?我可以得出结论,“上传”是上传到服务器的过程,而“下载”是上传到 AWS 的过程吗?

today I met this problem(so I'm writing in this topic...) Docs https://visionmedia.github.io/superagent/ clearly says, that今天我遇到了这个问题(所以我正在写这个主题......)文档https://visionmedia.github.io/superagent/清楚地说,那

event is direction: "upload" or "download"事件是方向:“上传”或“下载”

So IMHO you have to do something like:所以恕我直言,你必须做这样的事情:

        if(event.direction === "upload")
            console.log(e.direction,"is done",e.percent,"%")})

and it works so can setState() for progress bar here.它可以工作,因此可以在此处设置进度条的 setState() 。 It's very dummy, but well.这是非常虚拟的,但很好。 https://visionmedia.github.io/superagent/docs/test.html also here they are using this. https://visionmedia.github.io/superagent/docs/test.html他们也在使用这个。 Does anyone has better idea for this?有没有人对此有更好的主意?

I don't think there's an error.我不认为有错误。 That's how it's actually built, remember after sending a request to a server there's supposed to be a response, so that's where that download direction is coming from (as in downloading the data from the server as response data).这就是它的实际构建方式,记住在向服务器发送请求后应该有响应,所以这就是download方向的来源(如从服务器下载数据作为响应数据)。

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

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