1

I have looked around and looked at various tutorials on how to upload a file using node/express. I feel like I am doing something wrong on either the HTML or JQuery side. I am using the following link as a http://howtonode.org/really-simple-file-uploads.

However I am getting the error:

TypeError: Cannot read property 'fileUpload' of undefined    at module.exports.fileCreate 

Here is my code below:

uploadcontroller.js

fs.readFile(req.files.fileUpload.path, function (err, data) {
    var newPath = __dirname + "/uploads/" + imgString;
    fs.writeFile(newPath, data, function (err) {
    });
});

html snippet

<div class="form-group">
    <label for="fileUpload">Upload File</label>
    <input type="file" name="fileUpload" id="fileUpload">
</div>

I am using the Sails framework (not sure if that makes difference)

Edit: Complete Form

<form role="form" class="uploadFileForm">
    <div class="form-group">
        <label for="fileTitleInput">Title</label>
        <input type="text" name="formTitleInput" id="formTitleInput">
    </div>
    <div class="form-group">
        <label for="fileDescriptionInput">Description</label>
        <textarea class="form-control" rows="4" id="fileDescriptionInput"></textarea>
    </div>
<div class="form-group">
        <label for="fileUpload">Upload File</label>
        <input type="file" name="fileUpload" id="fileUpload">
    </div>
    <button type="submit" class="btn btn-default" id="file-submit-btn">Publish to Web</button>
</form>

3 Answers 3

3
app.post('/upload', function(req, res) {

    fs.readFile(req.files.image.path, function (err, data) {

        var imageName = req.files.image.name

        /// If there's an error
        if(!imageName){

            console.log("There was an error")
            res.redirect("/");
            res.end();

        } else {

          var newPath = __dirname + "/uploads/fullsize/" + imageName;

          /// write file to uploads/fullsize folder
          fs.writeFile(newPath, data, function (err) {

            /// let's see it
            res.redirect("/uploads/fullsize/" + imageName);

          });
        }
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

i have the "file reading" already in a module.exports called fileCreate. When I do an app.post in there i get an error saying "app is not defined"
0
app.post('/', function(req, res) {
  console.log(req.files);
  fs.readFile(req.files.displayImage.path, function (err, data) {

    var newPath = __dirname + "/uploads/"+req.files.displayImage.name;
    fs.writeFile(newPath, data, function (err) {
      if (err) throw err;
      res.redirect("back");
    });
  });
});

Just for your reference, "console.log(req.files)" would contain something like this:

{ displayImage:
   { domain: null,
     _events: null,
     _maxListeners: 10,
     size: 84654,
     path: 'E:\\Users\\xyz\\AppData\\Local\\Temp\\90020831e2b84acb2d4851e4d4
2d77d5',
     name: 'ccc - 1.jpg',
     type: 'image/jpeg',
     hash: false,
     lastModifiedDate: Wed May 22 2013 07:47:39 GMT+0530 (India Standard Time),
     _writeStream:
      { domain: null,
        _events: null,
        _maxListeners: 10,
        path: 'E:\\Users\\xyz\\AppData\\Local\\Temp\\90020831e2b84acb2d4851e
4d42d77d5',
        fd: 4,
        writable: false,
        flags: 'w',
        encoding: 'binary',
        mode: 438,
        bytesWritten: 84654,
        busy: false,
        _queue: [],
        _open: [Function],
        drainable: true },
     length: [Getter],
     filename: [Getter],
     mime: [Getter] }
}

3 Comments

when i do console.log(req.files), it returns undefined
@DavidMckee can you past your complete html FORM ? I think that you have an error when define FORM. Do you set "enctype='multipart/form-data'" in your form ? And why do you use fs.readFile/fs.writeFile when you can use fs.rename(origPath, newPath,function....) ?
ok i added my form as an edit. i didnt submit the enctype. i will try that and let you know
0

I ran into the same problem. Sails did not recognize req.files (undefined). So your problem seems very much Sails related. The following solved my problem (especially the Skipper documentation).

In the 0.9 version of Sails, you can uncomment this line in the config/express.js file: // bodyParser: require('express').bodyParser,

In the 0.10 version, use req.file instead of req.files. See their beta documentation on file uploads: http://beta.sailsjs.org/#/documentation/reference/Upgrading

Be sure to check out the Skipper documentation as well: https://github.com/balderdashy/skipper. Most likely your version of Sails will use this to process the file uploads.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.