Material-UI 按钮文件上传

3

我正在尝试通过按钮点击上传文件,但是遇到了一些问题。

                  <label>
                        <input
                        style={{ display: 'none' }}
                        type="file"
                        />     
                        <Button variant="contained" color="default">Upload</Button>
                    </label>

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

我正在使用React和Material-UI。


请参见:https://dev59.com/f1kR5IYBdhLWcg3w9RoO#54043619 - phoenix
这个回答解决了你的问题吗?如何在React的Material UI简单输入中启用文件上传? - James Mitofsky
1个回答

7

您可能会错过输入框的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>

1
我尝试了这个,但文件名不会显示。这是预期的行为吗? - fizmhd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接