вот как я думаю, что это должно go
import React from 'react';
export default class Upload extends React.Component<any, any> {
constructor(props: any) {
super(props);
this.state = {
selectedFile: null
};
}
onChangeHandler = (event: any) => {
this.setState({
selectedFile: event.target.files[0],
loaded: 0,
});
console.log(event.target.files[0]);
};
handleSubmit = (event: any) => {
event.preventDefault();
const formData = new FormData();
const { selectedFile } = this.state;
formData.append('inputFile', selectedFile);
fetch('/api/upload', {
method: 'POST',
body: formData,
});
};
render() {
return (
< div >
<form onSubmit={this.handleSubmit}>
<label>
Upload a file: <br /><br />
<input type="file" name="file" onChange={this.onChangeHandler} />
</label>
<br /><br />
<button type="submit">
Upload
</button>
</form >
</div >
);
}
}
, и с учетом вышесказанного API-интерфейс сервера может ожидать файл в каталоге файлов.
express пример
import express from 'express';
const fileupload = require("express-fileupload");
app.use(fileupload());
app.post('/api/upload', (req: any, res) => {
console.log(req.files);
// handle the file here
res.sendStatus(200);
});