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

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

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

问题

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

component.js

  1. import * as React from 'react';
  2. import { Button, Menu, MenuItem } from '@mui/material';
  3. import isEmpty from 'lodash/fp/isEmpty';
  4. import { styled, alpha } from '@mui/material/styles';
  5. import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown';
  6. export default function CustomizedMenus(props) {
  7. const [anchorEl, setAnchorEl] = React.useState(null);
  8. const open = Boolean(anchorEl);
  9. const handleClick = React.useCallback(event => {
  10. setAnchorEl(event.currentTarget);
  11. }, []);
  12. const handleClose = React.useCallback((hRef, fileName) => {
  13. if (!isEmpty(hRef) && !isEmpty(fileName) && fileName !== 'backdropClick') {
  14. const link = document.createElement('a');
  15. link.download = fileName;
  16. link.href = hRef;
  17. link.click();
  18. }
  19. setAnchorEl(null);
  20. }, []);
  21. return (
  22. <div>
  23. <Button
  24. id='customized-button'
  25. aria-controls={open ? 'customized-menu' : undefined}
  26. aria-haspopup='true'
  27. aria-expanded={open ? 'true' : undefined}
  28. variant='contained'
  29. disableElevation
  30. onClick={handleClick}
  31. endIcon={<KeyboardArrowDownIcon />}
  32. >
  33. 下载模板文件头
  34. </Button>
  35. <Menu
  36. id='customized-menu'
  37. MenuListProps={{
  38. 'aria-labelledby': 'customized-button',
  39. }}
  40. anchorEl={anchorEl}
  41. open={open}
  42. onClose={handleClose}
  43. >
  44. <MenuItem onClick={React.useCallback(() => handleClose('/data/User.xlsx', 'User'))} disableRipple>用户</MenuItem>
  45. <MenuItem onClick={React.useCallback(() => handleClose('/data/Employee.xlsx', '员工'))} disableRipple>员工</MenuItem>
  46. </Menu>
  47. </div>
  48. );
  49. }

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

英文:

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

  1. import * as React from &#39;react&#39;
  2. import { Button, Menu, MenuItem } from &#39;@mui/material&#39;
  3. import isEmpty from &#39;lodash/fp/isEmpty&#39;
  4. import { styled, alpha } from &#39;@mui/material/styles&#39;
  5. import KeyboardArrowDownIcon from &#39;@mui/icons-material/KeyboardArrowDown&#39;
  6. export default function CustomizedMenus(props) {
  7. const [ anchorEl, setAnchorEl ] = React.useState(null)
  8. const open = Boolean(anchorEl)
  9. const handleClick = React.useCallback(event =&gt; {
  10. setAnchorEl(event.currentTarget)
  11. }, [])
  12. const handleClose = React.useCallback((hRef, fileName) =&gt; {
  13. if (!isEmpty(hRef) &amp;&amp; !isEmpty(fileName) &amp;&amp; fileName !== &#39;backdropClick&#39;) {
  14. const link = document.createElement(&#39;a&#39;)
  15. link.download = fileName
  16. link.href = hRef
  17. link.click()
  18. }
  19. setAnchorEl(null)
  20. }, [])
  21. return (
  22. &lt;div&gt;
  23. &lt;Button
  24. id=&#39;customized-button&#39;
  25. aria-controls={open ? &#39;customized-menu&#39; : undefined}
  26. aria-haspopup=&#39;true&#39;
  27. aria-expanded={open ? &#39;true&#39; : undefined}
  28. variant=&#39;contained&#39;
  29. disableElevation
  30. onClick={handleClick}
  31. endIcon={&lt;KeyboardArrowDownIcon /&gt;}
  32. &gt;
  33. Download Template Header
  34. &lt;/Button&gt;
  35. &lt;Menu
  36. id=&#39;customized-menu&#39;
  37. MenuListProps={{
  38. &#39;aria-labelledby&#39;: &#39;customized-button&#39;,
  39. }}
  40. anchorEl={anchorEl}
  41. open={open}
  42. onClose={handleClose}
  43. &gt;
  44. &lt;MenuItem onClick={React.useCallback(() =&gt; handleClose(&#39;/data/User.xlsx&#39;, &#39;User&#39;))} disableRipple&gt;User&lt;/MenuItem&gt;
  45. &lt;MenuItem onClick={React.useCallback(() =&gt; handleClose(&#39;/data/Employee.xlsx&#39;, &#39;Employee&#39;))} disableRipple&gt;Employee&lt;/MenuItem&gt;
  46. &lt;/Menu&gt;
  47. &lt;/div&gt;
  48. )
  49. }

答案1

得分: 0

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

错误:

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

解决方案:

  1. <MenuItem onClick={React.useCallback(() => handleClose('/data/User.xlsx', 'User.xlsx'))} disableRipple>User</MenuItem>
  2. <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:

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

Solution:

  1. &lt;MenuItem onClick={React.useCallback(() =&gt; handleClose(&#39;/data/User.xlsx&#39;, &#39;User.xlsx&#39;))} disableRipple&gt;User&lt;/MenuItem&gt;
  2. &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:

确定