EDIT: === For clarity, I want to upload a file to the server, be it an image or a little .txt ===
I looked at other common problems like this, but could not alleviate my problem.
The goal is to create file upload functionality. The front end is as follows:
<div class="holdingDiv"> <form action="/file-upload" name="upload" class="dropzone dz-clickable" id="dropzoneArea" enctype="multipart/form-data"> <input style="display:none" type="file" name="thumbnail[thumbs]" /> <button id="uploadSubmitter" class="btn btn-primary btn-large" type="submit">Finished</button> </form> </div>
I have a form type and all the jazz settings.
The .js server processing the mail request is as follows:
app.post('/file-upload', imports.upload);
Please note that I also have the following:
//needed for forms app.use(express.bodyParser());
as well as the functionality needed to call the exports.upload export function.
The export.upload function looks something like this:
exports.upload = function (req, res) { console.log('FIRST TEST: ' + JSON.stringify(req.files)); console.log('second TEST: ' +req.files.thumbnail.thumbs.name); //get the file extenstion: //console.log('size' + req.files.thumbnail.size); // console.log('test this: ' + req.files.thumbnail.name); // var fileExtension = JSON.stringify(req.files); //console.log('Im getting this file type: '+ fileExtension.name); // console.log('this: '+req.files.upload); //fs.readFile(req.files.uploadFiles.path, function (err, data) { // // ... // var newPath = __dirname + "/uploads/"+uploadFiles.name; // fs.writeFile(newPath, data, function (err) { // res.redirect("back"); // }); //}); }
Many things are commented out as I tried to use different methods to make it work. I can call it using JSON Stringify as an entire object, but I would like it to be an object that I can plunge into and get information, for example, I would like to know the file type by dividing its name by ..:
req.files.thumbnail.thumbs.name
but when I try this (even JSON Stringyfied), it says it is undefined.
THINGS RECEIVED:
Moving the whole function to app.js (there is a small login function that works using req.body, I suggested that this might fix it.
using JSON Stringyfy to access specific parts of an object. (returns undefined)
smashing my head on the keyboard. (returns undefined)
changing the enctype form to a few different things, however most of the answers here say that form data is the best enctype for file uploads.
Any help and pointers as to why this is happening would be greatly appreciated!