javascript-如何在响应中的文件选择器中获取文件名?

前端之家收集整理的这篇文章主要介绍了javascript-如何在响应中的文件选择器中获取文件名? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

你能告诉我如何在响应文件选择器中获取文件名吗?
文件选择器中选择文件后,我试图在输入字段中设置值
这是我的代码
https://stackblitz.com/edit/react-d4kp1d?file=bulk.js
我尝试过这样

  1. <input
  2. id="file_input_file"
  3. className="none"
  4. type="file"
  5. ref={inputRef }
  6. onChange={(e)=>{
  7. console.log('---')
  8. console.log(inputRef.current[0].files[0].name)
  9. }}
  10. />

它给了我不确定的

最佳答案
良好的文档资料和示例摘自此处,解释了您要做什么.
https://reactjs.org/docs/uncontrolled-components.html#the-file-input-tag

代码笔:https://codepen.io/anon/pen/LaXXJj

React.JS包含要使用的特定文件API.

以下示例显示如何创建对DOM节点的引用以访问提交处理程序中的文件

HTML

  1. <input type="file" />

React.JS

  1. class FileInput extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. this.handleSubmit = this.handleSubmit.bind(this);
  5. this.fileInput = React.createRef();
  6. }
  7. handleSubmit(event) {
  8. event.preventDefault();
  9. alert(
  10. `Selected file - ${
  11. this.fileInput.current.files[0].name
  12. }`
  13. );
  14. }
  15. render() {
  16. return (
  17. <form onSubmit={this.handleSubmit}>
  18. <label>
  19. Upload file:
  20. <input type="file" ref={this.fileInput} />
  21. </label>
  22. <br />
  23. <button type="submit">Submit</button>
  24. </form>
  25. );
  26. }
  27. }
  28. ReactDOM.render(
  29. <FileInput />,document.getElementById('root')
  30. );

Alert Filename

  1. alert(`Selected file - ${this.fileInput.current.files[0].name}`);

引用:React.JS文档| Examples

猜你在找的JavaScript相关文章