简体   繁体   English

第二次上传文件不起作用

[英]Uploading file doesn't work on second time

I am trying to upload an excel file and send to api with reactjs, so far I can successfully send file to api.我正在尝试上传 excel 文件并使用 reactjs 发送到 api,到目前为止,我可以成功将文件发送到 Z248A5DAAZE70DC576 In my submit function, I also want to reset saved excel file from state when submitted, so I am doing as following在我提交的 function 中,我还想在提交时从 state 重置保存的 excel 文件,所以我正在执行以下操作

  const [urlImg, setURLImg] = useState("");
  const [excelFile, setExcelFile] = useState();

  function handleOnSubmit(e) {
    e.preventDefault();

    setExcelFile();
    setURLImg("");
  }

But it is not working when I upload for the second time with the same excel file as previous one.但是当我第二次使用与前一个相同的 excel 文件上传时它不起作用。 I console.log the state but not showing anything, it seems like the whole component is not being rendered when submit with the same excel file.console.log state 但没有显示任何内容,当使用相同的 excel 文件提交时,似乎没有呈现整个组件。 You can see my codesandbox sample here你可以在这里看到我的代码框示例

google drive link to download sample excel 谷歌驱动链接下载示例 excel

You can use the form's onSubmit event object to reset the file input field using the input's id property.您可以使用表单的onSubmit事件 object 使用输入的id属性重置文件输入字段。

function handleOnSubmit(e) {
  e.preventDefault();

  e.target["icon-button-file"].value = "";

  setExcelFile();
  setURLImg("");
}

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

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