I need to get a simple file upload solution for a new website. I am trying to Filepond for upload and a small program to save the file to the local file-system.
I have gone through the following code on a fresh Ubuntu LTS 18 machine.
https://codeunshackled.com/2018/11/03/React-File-Uploader-With-Node-Express/
Filepond provides no error details, giving the message "Error during upload". I have tried using the onerror event but console.log
doesn't seem to get triggered. The server and the frontend are both on the same Linux box on different ports.
There is an alternate server method using the so called PHP Boilterplate via vagrant. There the vm does not boot and it looks too cumbersome for a small file upload solution.
I have successfully used the following Nodejs code to upload the file and save it. However, if I try to point Filepond to this server, it just gives the same ambiguous message, "Error during upload".
var http = require('http');
var formidable = require('formidable');
var fs = require('fs');
http.createServer(function (req, res) {
if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
var oldpath = files.filetoupload.path;
var newpath = 'C:/Users/Your Name/' + files.filetoupload.name;
fs.rename(oldpath, newpath, function (err) {
if (err) throw err;
res.write('File uploaded and moved!');
res.end();
});
});
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
}).listen(8080);