2017-08-01 58 views
3

我试图访问HTMLElementInput.files.path发送到我的后端cloudinary进行处理。我遇到的问题是对象上现在有path键。无法访问文件上传路径密钥

enter image description here

我一直在读通过MDN Input page,我敢肯定我没有做错什么用的标记。下面是我拦截input上的数据的代码:

class App extends Component { 
    capture(e) { 
    e.preventDefault(); 
    const image = document.querySelector('input').files[0]; 
    console.log(image); 
    // image.path === undefined 
    } 
    render() { 
    return (
     <div className="App"> 
     <div className="App-header"> 
      <img src={logo} className="App-logo" alt="logo" /> 
      <h2>Welcome to React</h2> 
     </div> 
     <form onSubmit={e => this.capture(e)}> 
      <div> 
      <label htmlFor="photo_image">Upload an image</label> 
      <input id="photo_image" type="file" accept=".jpg, .jpeg, .png" /> 
      </div> 
      <button type="submit">Submit</button> 
     </form> 
     </div> 
    ); 
    } 
} 

export default App; 

我很难过。如果有人看到我要去的地方有错,或者有react,我不知道我是谁的耳朵!

更新

我以前离开了这个信息,我的道歉。该项目由create-react-app引导。我在开发过程中遇到了这个问题,以及在构建生产服务时遇到这个问题。

回答

0

我还是一个electron的心态,并没有想过这MDN告诉我真相是,浏览器不给我访问的硬盘路径出于安全原因,系统上的文件。这是在Reactiflux送给我的这个代码框中显示了获得最终的目标我一直在寻找,非常感谢@BTM此一方法:用ES6

https://codesandbox.io/s/lNN5pK6M

0

你缺少事件周围的方括号,而在捕捉事件提交。将其更改为下面的代码,并尝试:

<form onSubmit= {(e) => this.capture(e)}> 
+0

传递单,当你不需要括号论据 – rockchalkwushock

+1

感谢您更新我的知识。 –