图像文件的线性进度栏上传具有动态值MUI
我正在尝试上传图片,并根据上传的文件显示进度。 我正在为上传的每个文件登录, 假设我有7个文件并在上传时获取7个日志,我如何根据IT设置线性进度栏的百分比(移动进度)
import * as React from "react";
import LinearProgress, {
LinearProgressProps
} from "@mui/material/LinearProgress";
import Typography from "@mui/material/Typography";
import Box from "@mui/material/Box";
function LinearProgressWithLabel(
props: LinearProgressProps & { value: number }
) {
return (
<Box sx={{ display: "flex", alignItems: "center" }}>
<Box sx={{ width: "100%", mr: 1 }}>
<LinearProgress variant="determinate" {...props} />
</Box>
<Box sx={{ minWidth: 35 }}>
<Typography variant="body2" color="text.secondary">{`${Math.round(
props.value
)}%`}</Typography>
</Box>
</Box>
);
}
export default function LinearWithValueLabel() {
const [progress, setProgress] = React.useState(10);
React.useEffect(() => {
const timer = setInterval(() => {
setProgress((prevProgress) =>
prevProgress >= 100 ? 10 : prevProgress + 10
);
}, 800);
return () => {
clearInterval(timer);
};
}, []);
return (
<Box sx={{ width: "100%" }}>
<LinearProgressWithLabel value={progress} />
</Box>
);
}
codesandbox: https://codesandbox.io/s/nemeless-fast-8m1ien?file=/src/src/app.tsx:0-- 1124
I'm trying to upload pictures , and display progress according to files uploaded.
I'm getting log for every file uploaded,
Suppose i have 7 files and getting 7 logs while uploading, how can i set the percentage of linear progress bar according to it(move the progress)
import * as React from "react";
import LinearProgress, {
LinearProgressProps
} from "@mui/material/LinearProgress";
import Typography from "@mui/material/Typography";
import Box from "@mui/material/Box";
function LinearProgressWithLabel(
props: LinearProgressProps & { value: number }
) {
return (
<Box sx={{ display: "flex", alignItems: "center" }}>
<Box sx={{ width: "100%", mr: 1 }}>
<LinearProgress variant="determinate" {...props} />
</Box>
<Box sx={{ minWidth: 35 }}>
<Typography variant="body2" color="text.secondary">{`${Math.round(
props.value
)}%`}</Typography>
</Box>
</Box>
);
}
export default function LinearWithValueLabel() {
const [progress, setProgress] = React.useState(10);
React.useEffect(() => {
const timer = setInterval(() => {
setProgress((prevProgress) =>
prevProgress >= 100 ? 10 : prevProgress + 10
);
}, 800);
return () => {
clearInterval(timer);
};
}, []);
return (
<Box sx={{ width: "100%" }}>
<LinearProgressWithLabel value={progress} />
</Box>
);
}
codesandbox:https://codesandbox.io/s/nameless-fast-8m1ien?file=/src/App.tsx:0-1124
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用一个数组状态,该数组状态可以保留您上传的每个文件的进度。由于这是一个示例,默认状态已经由7个文件组成,其相应的进度。
更新该状态将看起来像这样,覆盖相应的文件进度
file1
:现在在您的
linearprogress
中,我们总结了所有进度, divide 要在州上载的文件数。由于这是一个部门,因此我们排除空对象状态并返回0。livedemo
You can make use of an array state which holds the progress of every file you upload. Since this is an example the default state already consists of 7 files with its corresponding progress.
Updating that state would look like this, overwriting the corresponding file progress
file1
:Now inside your
LinearProgress
we sum up all the progress and divide it by the number of files to be uploaded in the state. Since this is a division we exclude the empty object state and return 0.Live Demo