采用了棱角分明的js到sails.js服务器上传文件的相机(Upload camera file u

2019-10-21 03:11发布

我试图让采用了棱角分明一个简单的文件上传和sails.i是相当新的所有这些technolegies.could任何请你告诉我什么是错我的代码? 我创建使用命令的API调用的文件:“帆生成API文件”在我的帆服务器和控制器中,我将这些代码( https://github.com/sails101/file-uploads/blob/master/api/控制器/ FileController.js#L15 ):

 /**
       * FileController
       *
       * @description :: Server-side logic for managing files
       * @help        :: See http://links.sailsjs.org/docs/controllers
       */

      module.exports = {



        /**
         * `FileController.upload()`
         *
         * Upload file(s) to the server's disk.
         */
        upload: function (req, res) {

          // e.g.
          // 0 => infinite
          // 240000 => 4 minutes (240,000 miliseconds)
          // etc.
          //
          // Node defaults to 2 minutes.
          res.setTimeout(0);

          req.file('avatar')
          .upload({

            // You can apply a file upload limit (in bytes)
            maxBytes: 1000000

          }, function whenDone(err, uploadedFiles) {
            if (err) return res.serverError(err);
            else return res.json({
              files: uploadedFiles,
              textParams: req.params.all()
            });
          });
        },

        /**
         * `FileController.s3upload()`
         *
         * Upload file(s) to an S3 bucket.
         *
         * NOTE:
         * If this is a really big file, you'll want to change
         * the TCP connection timeout.  This is demonstrated as the
         * first line of the action below.
         */
        s3upload: function (req, res) {

          // e.g.
          // 0 => infinite
          // 240000 => 4 minutes (240,000 miliseconds)
          // etc.
          //
          // Node defaults to 2 minutes.
          res.setTimeout(0);

          req.file('avatar').upload({
            adapter: require('skipper-s3'),
            bucket: process.env.BUCKET,
            key: process.env.KEY,
            secret: process.env.SECRET
          }, function whenDone(err, uploadedFiles) {
            if (err) return res.serverError(err);
            else return res.json({
              files: uploadedFiles,
              textParams: req.params.all()
            });
          });
        },


        /**
         * FileController.download()
         *
         * Download a file from the server's disk.
         */
        download: function (req, res) {
          require('fs').createReadStream(req.param('path'))
          .on('error', function (err) {
            return res.serverError(err);
          })
          .pipe(res);
        }
      };

接下来,我做了一个函数,拍了一张照片,并显示it.now我想要做的就是将该文件发送到我的服务器和存储。 这里是我的代码:

   $scope.sendPost = function() {
    getPosts.getFoo('http://10.0.0.3:1337/user/create?name=temporaryUser&last_name=temporaryLastName4&title=' + 
      shareData.returnData()[0].title + '&content=' + shareData.returnData()[0].content + 
      '&image=' + /*shareData.returnData()[0].image*/ + '&category1=' + category1 + '&category2=' + 
      category2 + '&category3=' + category3 + '&category4=' + category4 + '&category5=' + category5 
      ).then(function(data) {
      $scope.items = data;
      console.log(shareData.returnData()[0]);
    });

      $scope.upload = function() {
          var url = 'http://localhost:1337/file/upload';
          var fd = new FormData();

          //previously I had this
          //angular.forEach($scope.files, function(file){
              //fd.append('image',file)
          //});

          fd.append('image', shareData.returnData()[0].image);

          $http.post(url, fd, {

              transformRequest:angular.identity,
              headers:{'Content-Type':undefined
              }
          })
          .success(function(data, status, headers){
              $scope.imageURL = data.resource_uri; //set it to the response we get
          })
          .error(function(data, status, headers){

          })
      }
    }

Answer 1:

你的大部分代码是正确的,我认为,但有一点是非常重要的,你应该给双方你的前端和后端的数据相同的“名称”。

在你的情况,你的后端代码中上传数据名称是“阿凡达”:

  req.file('avatar') .upload({ // You can apply a file upload limit (in bytes) maxBytes: 1000000 }, function whenDone(err, uploadedFiles) { if (err) return res.serverError(err); else return res.json({ files: uploadedFiles, textParams: req.params.all() }); }); 

然而,前端码内的后数据的名称是“图像”。

fd.append('image', shareData.returnData()[0].image);

我觉得这是应该停止哪些应用程序中的主要问题。

下面是如何使用的角度来远程上传文件超过API的工作版本http://jsfiddle.net/JeJenny/ZG9re/这是为我工作。

下面是我的后端代码你参考:

 // myApp/api/controllers/FileController.js module.exports = { upload: function(req, res) { var uploadFile = req.file('file') //console.log(uploadFile); uploadFile.upload({ dirname: sails.config.zdbconf.attachmentPath, maxBytes: 100000000 }, function(err, files) { if (err) return res.serverError(err); return res.json({ message: files.length + ' file(s) uploaded successfully!', files: files }); }); } }; 



文章来源: Upload camera file using angular js to sails.js server