【发布时间】:2017-05-18 02:50:50
【问题描述】:
我有一个简单的表单来上传文件,稍后将处理我的后端 python 代码。但是,当我尝试上传文件时得到的是 C:\fakepath\test.txt 。
从我所做的研究来看,出于安全考虑,这是意料之中的。这很好,但现在我的问题是,我该如何解决这个问题才能使用我在后端上传的文件?
我看过很多不同的地方,但似乎没有一个能解决这个问题。
这是我当前的代码:
class SomeForm extends Component{
handleFile(e){
this.setState({value: e.target.value});
}
handleSubmit(e){
var me=this;
if (this.state.value.length>0){
var upload_file = this.state.value;
const request = axios.post(this.props.cfg_url+'/upload', {upload_file})
.then(function(response){
console.log('successfully uploaded', upload_file);
})
}
}
render(){
return(
<Form inline onSubmit={this.handleSubmit}>
<FormGroup controlId='uploadFormId'>
<ControlLabel>Upload File:</ControlLabel>
<FormControl
type='file'
label='File'
onChange={this.props.onChange}
/>
</FormGroup>
<Button type='submit'>Upload</Button>
</Form>
);
}
}
【问题讨论】:
-
为什么需要路径?
-
@epascarello 因为我必须在 Python 方面进行一些文件操作。所以我需要它的路径来打开、读取、写入任何内容。除非我不这样做,否则请告诉我如何
-
但是 URL 与文件无关,这不是您访问文件详细信息的方式。您将文件错误地提交到服务器。见:stackoverflow.com/questions/2320069/jquery-ajax-file-upload
标签: javascript node.js file reactjs react-bootstrap