Я использую React Dropzone для получения файла от пользователя, а в обратном вызове я использую reader.readAsDataURL(...)
:
const onDrop = useCallback(acceptedFiles => {
// this callback will be called after files get dropped, we will get the acceptedFiles. If you want, you can even access the rejected files too
console.log(acceptedFiles);
const axiosInstance = axios.create({
baseURL: "http://localhost:5000"
});
const reader = new FileReader();
reader.onabort = () => console.log("file reading was aborted");
reader.onerror = () => console.log("file reading has failed");
reader.onload = async () => {
// Do whatever you want with the file contents
const result = reader.result;
const body = {
url: result
};
await axiosInstance.post("/api/upload", body);
};
acceptedFiles.forEach(file => reader.readAsDataURL(file));
}, []);
Однако сервер не может обработать запрос, так как он слишкомlong:
PayloadTooLargeError: request entity too large : Failed to load resource: the server responded with a status of 413 (Payload Too Large)
.
Как я могу отправить файл с клиента React на сервер Node?
Вот мой сервер:
app.use("/api/upload",require("./routes/api/upload-clients"));
// upload-clients.js
const express = require("express");
const router = express.Router();
const multer = require("multer");
router.post("/", async (req, res) => {
var d = new Date();
console.log(
"Starting the upload the file to the server ..." +
d.getHours() +
":" +
d.getMinutes() +
":" +
d.getSeconds()
);
console.log(req.body);
// TODO
});
module.exports = router;