javascript - AngularJS + ng文件上传+ Sails JS

时间:2016-08-29 01:15:40

标签: javascript angularjs node.js sails.js ng-file-upload

我需要将这些信息与我上传的每个文件一起发送。文档说我可以使用data参数实现,但是我无法通过我的Sails控制器中的操作访问它。

前端:

Upload.upload({ url: '/file/upload', arrayKey: '', data: { file: $scope.files, otherInfo: {user: user, person: 12 }}}).....

Sails JS:

req.file('file').upload({ dirname: '../../websrc/uploads' }, function(err, files) {
      if (err) return res.serverError(err);
      return res.json({
        message: files.length + ' file(s) uploaded successfully!',
        files: files
      });
    });

如果我记录文件,我会看到这样的X对象:

UploadedFileMetadata[0]
extra: undefined
fd: "/xxxxxxxxxxxxxx/uploads/24fd1f66-36df-40ec-bed3-45e18df77469.jpg"
field: "file"
filename: "xxxxxxxxxxxxxxxxxxxxxx.jpg"
size: 199247
status: "bufferingOrWriting"
stream: PassThrough
type: "image/jpeg"

如何为每个文件发送信息并从我的服务器访问它?

1 个答案:

答案 0 :(得分:0)

 $scope.uploadFiles = function () {

            var request = {
                method: 'POST',
                url: '/api/fileupload/',
                data: formdata,
                headers: {
                    'Content-Type': undefined
                }
            };

            // SEND THE FILES.
            $http(request)
                .success(function (d) {
                    alert(d);
                })
                .error(function () {
                });