Вы можете попробовать следующий метод, который вы можете использовать multer с express для обработки загруженных данных файла.
Файл реакции
import React, { Component } from "react";
import ReactDOM from "react-dom";
import axios from "axios";
class App extends Component {
handleFileChange = e => {
this.setState({ file: e.target.files[0] });
};
handleChange = e => {
this.setState({ text: e.target.value });
};
upload = () => {
if (this.state.file) {
let data = new FormData();
data.append("file", this.state.file);
data.set("data", this.state.text);
axios
.post("http://yourhost/file", data)
.then(response => console.log(response))
.catch(error => console.log(error));
}
};
render() {
return (
<div>
<input type="text" onChange={this.handleChange} />
<input type="file" onChange={this.handleFileChange} />
<input type="button" onClick={this.upload} value="Upload" />
</div>
);
}
}
export defaults App;
Экспресс-сервер
const express = require('express');
const app =express();
const path = require('path');
const cors = require('cors')();
const bodyParser = require('body-parser');
const multer = require('multer')
const port =process.env.PORT || 3000;;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended:true
}));
app.use(cors);
const storage = multer.diskStorage({
destination: __dirname +'/media/',
filename(req, file, cb) {
console.log(file);
cb(null, `${file.originalname}-${new Date()}`);
}
});
const upload = multer({ storage });
app.post('/file',upload.single('file'), function(req, res) {
console.log(req.body.data);
console.log(req.files);
});
app.listen(port,()=> console.log('Running on port: '+port));