使用 reactjs 上传文件并处理 C:/fakepath/file
Uploading a file with reactjs and dealing with C:/fakepath/file
我有一个简单的表单来上传文件,稍后将由我的后端 python 代码处理。但是,当我尝试上传文件时得到的是 C:\fakepath\test.txt .
根据我所做的研究,这是出于安全考虑而预期和完成的。这很好,但现在我的问题是,我究竟该如何解决这个问题才能使用我在后端上传的文件?
我看了很多不同的地方,none 似乎解决了这个问题。
这是我当前的代码:
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>
);
}
}
我不明白为什么要设置 var upload_file = this.state.value;
但您从未在状态对象中分配 value
(在下面的示例中)。
我认为您正在使用 input['file']
的 value
属性 而不是 files
one. You have to take the selected file using the files
property and use the FormData interface 来映射表单参数。
class SomeForm extends Component {
handleSubmit(e){
if (e.target.input.files.length) {
const upload_file = e.target.input.files[0];
const formData = new FormData();
formData.append('file', upload_file);
const request = axios.post(this.props.cfg_url+'/upload', formData)
.then(function(response){
console.log('successfully uploaded', upload_file);
});
} else {
console.log('You need to select a file');
}
}
render(){
return(
<Form inline onSubmit={this.handleSubmit}>
<FormGroup controlId='uploadFormId'>
<ControlLabel>Upload File:</ControlLabel>
<FormControl
type='file'
name="input-file"
label='File'
/>
</FormGroup>
<Button type='submit'>Upload</Button>
</Form>
);
}
}
来源:https://github.com/mzabriskie/axios/tree/master/examples/upload
原因是因为您使用的是 this.setState({value: e.target.value})
,它只会使用假路径字符串更新值,而不是实际的 DOM 元素。
我试图在 React 中上传一个文件,该文件将用作使用 fetch 的 GET 请求的主体。我的获取请求失败,因为正文有效负载是一个字符串 "C:/fakepath/file"
以下是如何使用 useRef 和 useEffect 挂钩上传文件。在下面的示例中,我将文件传递给 API request
的自定义挂钩
export function App(){
const [file, setFiles] = useState(null)
const inputRef = useRef()
useCustomFetchHook(file)
return(
<div>
<input
type="file" id="input"
// onChange={ e => setFiles(e.target.value)}
onChange={() => setFiles(inputRef.current.files[0])}
ref={inputRef}
/>
</div>
)
}
希望它对其他在 React 中使用上传文件时遇到 "C:/fakepath/file" 问题并遇到此 Whosebug 的人有所帮助 post 寻找解决方案
SM
const formData = new FormData();
formData.append("文件", imagestudent[0]);
Axios
.post('url',
formData
,
{
"headers" :
{
"Content-Type":"multipart/form-data",
}
}
)
.then(
res => {
console.log(res.data)
}
)
对于输入
onChange={e => setimagestudent(e.target.files)} />
我有一个简单的表单来上传文件,稍后将由我的后端 python 代码处理。但是,当我尝试上传文件时得到的是 C:\fakepath\test.txt .
根据我所做的研究,这是出于安全考虑而预期和完成的。这很好,但现在我的问题是,我究竟该如何解决这个问题才能使用我在后端上传的文件?
我看了很多不同的地方,none 似乎解决了这个问题。
这是我当前的代码:
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>
);
}
}
我不明白为什么要设置 var upload_file = this.state.value;
但您从未在状态对象中分配 value
(在下面的示例中)。
我认为您正在使用 input['file']
的 value
属性 而不是 files
one. You have to take the selected file using the files
property and use the FormData interface 来映射表单参数。
class SomeForm extends Component {
handleSubmit(e){
if (e.target.input.files.length) {
const upload_file = e.target.input.files[0];
const formData = new FormData();
formData.append('file', upload_file);
const request = axios.post(this.props.cfg_url+'/upload', formData)
.then(function(response){
console.log('successfully uploaded', upload_file);
});
} else {
console.log('You need to select a file');
}
}
render(){
return(
<Form inline onSubmit={this.handleSubmit}>
<FormGroup controlId='uploadFormId'>
<ControlLabel>Upload File:</ControlLabel>
<FormControl
type='file'
name="input-file"
label='File'
/>
</FormGroup>
<Button type='submit'>Upload</Button>
</Form>
);
}
}
来源:https://github.com/mzabriskie/axios/tree/master/examples/upload
原因是因为您使用的是 this.setState({value: e.target.value})
,它只会使用假路径字符串更新值,而不是实际的 DOM 元素。
我试图在 React 中上传一个文件,该文件将用作使用 fetch 的 GET 请求的主体。我的获取请求失败,因为正文有效负载是一个字符串 "C:/fakepath/file"
以下是如何使用 useRef 和 useEffect 挂钩上传文件。在下面的示例中,我将文件传递给 API request
的自定义挂钩export function App(){
const [file, setFiles] = useState(null)
const inputRef = useRef()
useCustomFetchHook(file)
return(
<div>
<input
type="file" id="input"
// onChange={ e => setFiles(e.target.value)}
onChange={() => setFiles(inputRef.current.files[0])}
ref={inputRef}
/>
</div>
)
}
希望它对其他在 React 中使用上传文件时遇到 "C:/fakepath/file" 问题并遇到此 Whosebug 的人有所帮助 post 寻找解决方案
SM
const formData = new FormData(); formData.append("文件", imagestudent[0]);
Axios
.post('url',
formData
,
{
"headers" :
{
"Content-Type":"multipart/form-data",
}
}
)
.then(
res => {
console.log(res.data)
}
)
对于输入
onChange={e => setimagestudent(e.target.files)} />