使用SAILJS POST和GET图像不起作用(错误:[object,Object])上传图片

时间:2016-04-15 04:27:00

标签: javascript node.js sails.js sails-mongo

这是我的代码,它被称为sails文档我想上传文件控制器操作的图像,并通过用户控制器操作上传和获取它上传和存储它。它没有显示它的节目

error: [object, Object].

文件控制器操作 用于上传图片

// 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

0 个答案:

没有答案