MEAN堆栈文件上传

时间:2014-04-08 15:31:11

标签: node.js angularjs mongodb express mean-stack

我最近开始使用MEAN Stack进行编程,而我目前正在实施某种社交网络。一直在使用MEAN.io框架来做到这一点。 我现在的主要问题是让文件上传工作,因为我想要做的是从表单接收文件到AngularJS控制器并将其与更多信息一起传递给ExpressJS,这样我最终可以将所有内容发送到MongoDB的。 (我正在建立一个注册新用户表单。)

我不想将文件本身存储在数据库中,但我想存储一个链接。

我在google上用不同的搜索查询搜索了几十页,但我找不到任何我能理解或工作的内容。一直在寻找没有结果的时间。这就是我来这里的原因。

任何人都可以帮我吗?

谢谢:)

编辑:也许一些代码可以帮助理解。

我使用的默认MEAN.io用户Angular控制器具有以下功能:

$scope.register = function(){
        $scope.usernameError = null;
        $scope.registerError = null;
        $http.post('/register', {
            email: $scope.user.email,
            password: $scope.user.password,
            confirmPassword: $scope.user.confirmPassword,
            username: $scope.user.username,
            name: $scope.user.fullname
        })//... has a bit more code but I cut it because the post is the main thing here.
    };

我想做的是: 从表单接收文件到此控制器并将其与电子邮件,密码,名称等一起传递,并且能够使用位于服务器端的expressjs上的json。 ' /注册'是一个nodejs路由,所以服务器控制器创建用户(使用用户模式)并将其发送到MongoDB。

4 个答案:

答案 0 :(得分:25)

我最近做了这样的事情。我用了angular-file-upload。您还希望端点能够node-multiparty来解析表单数据。然后,您可以使用s3将文件上传到s3。

这是我编辑过的一些代码。

角度模板

<button>
  Upload <input type="file" ng-file-select="onFileSelect($files)">
</button>

角度控制器

$scope.onFileSelect = function(image) {
  $scope.uploadInProgress = true;
  $scope.uploadProgress = 0;

  if (angular.isArray(image)) {
    image = image[0];
  }

  $scope.upload = $upload.upload({
    url: '/api/v1/upload/image',
    method: 'POST',
    data: {
      type: 'profile'
    },
    file: image
  }).progress(function(event) {
    $scope.uploadProgress = Math.floor(event.loaded / event.total);
    $scope.$apply();
  }).success(function(data, status, headers, config) {
    AlertService.success('Photo uploaded!');
  }).error(function(err) {
    $scope.uploadInProgress = false;
    AlertService.error('Error uploading file: ' + err.message || err);
  });
};

<强>路线

var uuid = require('uuid'); // https://github.com/defunctzombie/node-uuid
var multiparty = require('multiparty'); // https://github.com/andrewrk/node-multiparty
var s3 = require('s3'); // https://github.com/andrewrk/node-s3-client

var s3Client = s3.createClient({
  key: '<your_key>',
  secret: '<your_secret>',
  bucket: '<your_bucket>'
});

module.exports = function(app) {
  app.post('/api/v1/upload/image', function(req, res) {
    var form = new multiparty.Form();
    form.parse(req, function(err, fields, files) {
      var file = files.file[0];
      var contentType = file.headers['content-type'];
      var extension = file.path.substring(file.path.lastIndexOf('.'));
      var destPath = '/' + user.id + '/profile' + '/' + uuid.v4() + extension;

      var headers = {
        'x-amz-acl': 'public-read',
        'Content-Length': file.size,
        'Content-Type': contentType
      };
      var uploader = s3Client.upload(file.path, destPath, headers);

      uploader.on('error', function(err) {
        //TODO handle this
      });

      uploader.on('end', function(url) {
        //TODO do something with the url
        console.log('file opened:', url);
      });
    });
  });
}

我从代码中更改了此内容,因此可能无法开箱即用,但希望它有用!

答案 1 :(得分:3)

最近在mean.io上的软件包列表中添加了一个新软件包。这是一个美女!

简单地运行:

$ mean install mean-upload

这会将软件包安装到节点文件夹中,但您可以访问软件包中的指令。

http://mean.io/#!/packages/53ccd40e56eac633a3eee335

在表单视图中,添加如下内容:

    <div class="form-group">
        <label class="control-label">Images</label>
        <mean-upload file-dest="'/packages/photos/'" upload-file-callback="uploadFileArticleCallback(file)"></mean-upload>
        <br>
        <ul class="list-group">
            <li ng-repeat="image in article.images" class="list-group-item">
                {{image.name}}
                <span class="glyphicon glyphicon-remove-circle pull-right" ng-click="deletePhoto(image)"></span>
            </li>
        </ul>
    </div>

在你的控制器中:

    $scope.uploadFileArticleCallback = function(file) {
      if (file.type.indexOf('image') !== -1){
          $scope.article.images.push({
            'size': file.size,
            'type': file.type,
            'name': file.name,
            'src': file.src
          });
      }
      else{
          $scope.article.files.push({
            'size': file.size,
            'type': file.type,
            'name': file.name,
            'src': file.src
          });
      }
    };

    $scope.deletePhoto = function(photo) {
        var index = $scope.article.images.indexOf(photo);
        $scope.article.images.splice(index, 1);
    }

享受!

答案 2 :(得分:0)

平均上传已经过时,现在称为&#34;上传&#34;。它在 - https://git.mean.io/orit/upload

中管理

答案 3 :(得分:0)

我知道这篇文章很老了。我遇到了它并且@kentcdodds得到了我真正喜欢的答案,但他使用的图书馆现在已经过时了,我无法让他们工作。因此,经过一些研究,我想要分享一个更新的类似解决方案。

HTML使用ng-upload

<form >
    <div style="margin-bottom: 15px;">
        <button type="file" name="file" id="file" ngf-select="uploadFiles($file, $invalidFiles)" accept="image/*" ngf-max-height="1000" ngf-max-size="1MB">Select File</button>
    </div>
</form>

包含上传模块

下载它,引用文件并包含模块

var app = angular.module('app', ['ngFileUpload']);

这将允许您访问Upload服务。

控制器代码

$scope.uploadFiles = function(file, errFiles) {
    $scope.f = file;
    $scope.errFile = errFiles && errFiles[0];
    if (file) {
        file.upload = Upload.upload({
            url: 'you-api-endpoint',
            data: {file: file}
        });

        //put promise and event watchers here if wanted
    }   
};

NODE api代码

以下所有代码都在一个单独的route文件中,该文件在我的主server.js文件中为required

require('./app/app-routes.js')(app, _);

var fs = require('fs');
var uuid = require('uuid');
var s3 = require('s3fs'); // https://github.com/RiptideElements/s3fs
var s3Impl = new s3('bucketname', {
    accessKeyId: '<your access key id>',
    secretAccessKey: '< your secret access key >'
});

var multiparty = require('connect-multiparty');
var multipartyMiddleware = multiparty();

module.exports = function(app, _) {
    app.use(multipartyMiddleware);


    app.post('/your-api-endpoint',function(req, res){

    var file = req.files.file; // multiparty is what allows the file to to be accessed in the req
    var stream = fs.createReadStream(file.path);
    var extension = file.path.substring(file.path.lastIndexOf('.'));
    var destPath = '/' + req.user._id + '/avatar/' +  uuid.v4() + extension;
    var base = 'https://you-bucket-url';
    return s3Impl.writeFile(destPath, stream, {ContentType: file.type}).then(function(one){
        fs.unlink(file.path);
        res.send(base + destPath); 
    });
});

我试图做的就是为用户上传一个独特的头像。希望这会有所帮助!!