如何在axios中获取onUploadProgress? [英] how to get onUploadProgress in axios?

查看:1538
本文介绍了如何在axios中获取onUploadProgress?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑如何使用axios上传进度事件.实际上,我正在将大量文件存储到AWS s3中.为此,如何获得上传进度?我需要此功能onUploadProgress

I'm little bit confused that how to upload progress event with axios. Actually I am storing huge number files into aws s3. For that, how to get uploaded progress? I need this function onUploadProgress

目前,我的发帖请求是这样的:

Currently my Post request is like this:

export function uploadtoCdnAndSaveToDb(data) {
    return dispatch => {
        dispatch(showUnderLoader(true));
        return axios.post('/posttodb', {data:data},

        ).then((response) => {
            console.log(response.data)
        })
    }
}

推荐答案

Axios存储库中有一个明确的示例,说明了如何执行此操作:

The Axios repository has a clear example on how to do this: https://github.com/mzabriskie/axios/blob/master/examples/upload/index.html

网站摘录

使用axios发出请求时,可以传入请求配置.该请求配置的一部分是上载进度时调用的函数.

When you make a request with axios, you can pass in request config. Part of that request config is the function to call when upload progresses.

const config = {
    onUploadProgress: progressEvent => console.log(progressEvent.loaded)
}

使用axios发出请求时,可以传入此配置对象.

When you make the request using axios, you can pass in this config object.

axios.put('/upload/server', data, config)

只要上传进度发生变化,就会调用此函数.

And this function will be called whenever the upload progress changes.

只需在您的代码上做个注释

我还注意到您没有充分利用ES6!

I also noticed that you're not using ES6 to its fullest potential!

对象声明

对于这样的东西,它有一些不错的语法,例如,您定义了一个对象,例如:{ data: data },但是{ data }就足够了.

It's got some nice syntax for stuff like this, for example, you have defined an object like: { data: data }, but { data } is sufficient.

使用带有一些掉毛规则的短绒棉袜是值得的,最常见的是 AirBnB样式指南

It might be worth using a linter with some linting rules, with the most common being the AirBnB style guide

这篇关于如何在axios中获取onUploadProgress?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆