Material-UI 文件上传按钮

g14*_*14u 3 reactjs material-ui

我正在尝试通过单击按钮上传文件,但是存在一些问题。

                  <label>
                        <input
                        style={{ display: 'none' }}
                        type="file"
                        />     
                        <Button variant="contained" color="default">Upload</Button>
                    </label>
Run Code Online (Sandbox Code Playgroud)

这是我的代码,但单击按钮时文件上传不起作用,因此未检测到输入。我该如何解决这个问题?

我正在使用 React 和 Material-UI。

Vie*_*inh 8

您可能会错过输入的 id:

让我们尝试一下:

  <input
    style={{ display: "none" }}
    id="contained-button-file"
    type="file"
  />
  <label htmlFor="contained-button-file">
    <Button variant="contained" color="primary" component="span">
      Upload
    </Button>
  </label>
</div>
Run Code Online (Sandbox Code Playgroud)