无法从下拉菜单中使用material-ui和reactjs下载Excel文件

huangapple go评论72阅读模式
英文:

Unable to download excel file from dropdown menu using material-ui and reactjs

问题

我试图从公共文件夹 (public/data/User.xlsx) 下载文件,但我无法下载文件。我在此实现中使用了 material-ui 和 react.js。

component.js

import * as React from 'react';
import { Button, Menu, MenuItem } from '@mui/material';
import isEmpty from 'lodash/fp/isEmpty';
import { styled, alpha } from '@mui/material/styles';
import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown';

export default function CustomizedMenus(props) {
    const [anchorEl, setAnchorEl] = React.useState(null);
    const open = Boolean(anchorEl);

    const handleClick = React.useCallback(event => {
        setAnchorEl(event.currentTarget);
    }, []);
    const handleClose = React.useCallback((hRef, fileName) => {
        if (!isEmpty(hRef) && !isEmpty(fileName) && fileName !== 'backdropClick') {
            const link = document.createElement('a');
            link.download = fileName;
            link.href = hRef;
            link.click();
        }
        setAnchorEl(null);
    }, []);

    return (
        <div>
            <Button
                id='customized-button'
                aria-controls={open ? 'customized-menu' : undefined}
                aria-haspopup='true'
                aria-expanded={open ? 'true' : undefined}
                variant='contained'
                disableElevation
                onClick={handleClick}
                endIcon={<KeyboardArrowDownIcon />}
            >
                下载模板文件头
            </Button>
            <Menu
                id='customized-menu'
                MenuListProps={{
                    'aria-labelledby': 'customized-button',
                }}
                anchorEl={anchorEl}
                open={open}
                onClose={handleClose}
            >
                <MenuItem onClick={React.useCallback(() => handleClose('/data/User.xlsx', 'User'))} disableRipple>用户</MenuItem>
                <MenuItem onClick={React.useCallback(() => handleClose('/data/Employee.xlsx', '员工'))} disableRipple>员工</MenuItem>
            </Menu>
        </div>
    );
}

这是你提供的代码的中文翻译部分。

英文:

I'm trying to download file from public folder (public/data/User.xlsx) but I'm not able to download file. I'm using material-ui and react.js for this implementation

component.js

import * as React from &#39;react&#39;
import { Button, Menu, MenuItem } from &#39;@mui/material&#39;
import isEmpty from &#39;lodash/fp/isEmpty&#39;
import { styled, alpha } from &#39;@mui/material/styles&#39;
import KeyboardArrowDownIcon from &#39;@mui/icons-material/KeyboardArrowDown&#39;
export default function CustomizedMenus(props) {
const [ anchorEl, setAnchorEl ] = React.useState(null)
const open = Boolean(anchorEl)
const handleClick = React.useCallback(event =&gt; {
setAnchorEl(event.currentTarget)
}, [])
const handleClose = React.useCallback((hRef, fileName) =&gt; {
if (!isEmpty(hRef) &amp;&amp; !isEmpty(fileName) &amp;&amp; fileName !== &#39;backdropClick&#39;) {
const link = document.createElement(&#39;a&#39;)
link.download = fileName
link.href = hRef
link.click()
}
setAnchorEl(null)
}, [])
return (
&lt;div&gt;
&lt;Button
id=&#39;customized-button&#39;
aria-controls={open ? &#39;customized-menu&#39; : undefined}
aria-haspopup=&#39;true&#39;
aria-expanded={open ? &#39;true&#39; : undefined}
variant=&#39;contained&#39;
disableElevation
onClick={handleClick}
endIcon={&lt;KeyboardArrowDownIcon /&gt;}
&gt;
Download Template Header
&lt;/Button&gt;
&lt;Menu
id=&#39;customized-menu&#39;
MenuListProps={{
&#39;aria-labelledby&#39;: &#39;customized-button&#39;,
}}
anchorEl={anchorEl}
open={open}
onClose={handleClose}
&gt;
&lt;MenuItem onClick={React.useCallback(() =&gt; handleClose(&#39;/data/User.xlsx&#39;, &#39;User&#39;))} disableRipple&gt;User&lt;/MenuItem&gt;
&lt;MenuItem onClick={React.useCallback(() =&gt; handleClose(&#39;/data/Employee.xlsx&#39;, &#39;Employee&#39;))} disableRipple&gt;Employee&lt;/MenuItem&gt;
&lt;/Menu&gt;
&lt;/div&gt;
)
}

答案1

得分: 0

I have added file extension to the file name it working as I expected way

错误:

<MenuItem onClick={React.useCallback(() => handleClose('/data/User.xlsx', 'User'))} disableRipple>User</MenuItem>
<MenuItem onClick={React.useCallback(() => handleClose('/data/Employee.xlsx', 'Employee'))} disableRipple>Employee</MenuItem>

解决方案:

<MenuItem onClick={React.useCallback(() => handleClose('/data/User.xlsx', 'User.xlsx'))} disableRipple>User</MenuItem>
<MenuItem onClick={React.useCallback(() => handleClose('/data/Employee.xlsx', 'Employee.xlsx'))} disableRipple>Employee</MenuItem>
英文:

I have added file extension to the file name it working as I expected way

Error:

&lt;MenuItem onClick={React.useCallback(() =&gt; handleClose(&#39;/data/User.xlsx&#39;, &#39;User&#39;))} disableRipple&gt;User&lt;/MenuItem&gt;
&lt;MenuItem onClick={React.useCallback(() =&gt; handleClose(&#39;/data/Employee.xlsx&#39;, &#39;Employee&#39;))} disableRipple&gt;Employee&lt;/MenuItem&gt;

Solution:

&lt;MenuItem onClick={React.useCallback(() =&gt; handleClose(&#39;/data/User.xlsx&#39;, &#39;User.xlsx&#39;))} disableRipple&gt;User&lt;/MenuItem&gt;
&lt;MenuItem onClick={React.useCallback(() =&gt; handleClose(&#39;/data/Employee.xlsx&#39;, &#39;Employee.xlsx&#39;))} disableRipple&gt;Employee&lt;/MenuItem&gt;

huangapple
  • 本文由 发表于 2023年5月26日 15:43:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定