0

This is my code which is referred to sails documentation I want to upload an image for file controller action and get it upload and store it through user controller action upload and get. It is not working its show an

error: [object, Object].

file controller action for uploading image

// myApp/api/controllers/FileController.js

module.exports = {

  index: function (req, res) {

    res.writeHead(200, {
      'content-type': 'text/html'
    });
    res.end(
      //'<form action="http://localhost:1337/file/upload" enctype="multipart/form-data" method="post">'+
      '<form action="http://localhost:1337/user/avatar" enctype="multipart/form-data" method="post">' +
      '<input type="text" name="title"><br>' +
      '<input type="file" name="avatar" multiple="multiple"><br>' +
      '<input type="submit" value="Upload">' +
      '</form>'
    )
  },
  /**
   * User Controller action
   * for upload andget image
   * Upload avatar for currently logged-in user
   *
   * (POST /user/avatar)
   */
  uploadAvatar: function (req, res) {
    console.log('avatar', req.method);
    console.log(req.session.me);
    req.file('avatar').upload({
      // don't allow the total upload size to exceed ~10MB
      maxBytes: 10000000
    }, function whenDone(err, uploadedFiles) {
      if (err) {
        return res.negotiate(err);
      }
      console.log(uploadedFiles);

      // If no files were uploaded, respond with an error.
      if (uploadedFiles.length === 0) {
        return res.badRequest('No file was uploaded');
      }


      // Save the "fd" and the url where the avatar for a user can be accessed
      User.update(req.session.me, {

          // Generate a unique URL where the avatar can be downloaded.
          avatarUrl: require('util').format('%s/user/avatar/%s', sails.getBaseUrl(), req.session.me),

          // Grab the first file and use it's fd (file descriptor)
          avatarFd: uploadedFiles[0].fd
        })
        .exec(function (err) {
          if (err) return res.negotiate(err);
          return res.ok();
        });
    });
  },


  /**
   * Download avatar of the user with the specified id
   *
   * (GET /user/avatar/:id)
   */
  avatar: function (req, res) {

    console.log('avatar', req.method);
    console.log(req.session.me);
    req.validate({
      //id: 'string'
      id: '57053900e15832cc51b84c47'
    });

    User.findOne(req.param('id')).exec(function (err, user) {


      if (err) return res.negotiate(err);
      if (!user) return res.notFound();

      // User has no avatar image uploaded.
      // (should have never have hit this endpoint and used the default image)
      if (!user.avatarFd) {
        return res.notFound();
      }

      var SkipperDisk = require('skipper-disk');
      var fileAdapter = SkipperDisk(/* optional opts */);

      // Stream the file down
      fileAdapter.read(user.avatarFd)
        .on('error', function (err) {
          return res.serverError(err);
        }).pipe(res);
    });
  }
};

File controller action Error msg 1 2 3 4

Community
  • 1
  • 1

0 Answers0