angularjs - 如何在请求中发送额外数据?

标签 angularjs node.js

我想在http请求中发送itemId。如何做到这一点以及我将在服务器请求中看到什么?

这是请求:

var fd = new FormData();
var itemId = scope.vm.item._id;

fd.append('file', scope.files[0]);
$http.post('http://localhost:8090/file-upload', fd, {
   transformRequest: angular.identity,
   headers: {
      'Content-Type': undefined
   }
});

服务器

 app.post('/file-upload', function(req, res, next) {
console.log("received file");
var pathFile = ' ';
var wId = "itemId";

var storage = multer.diskStorage({
    destination: function (req, file, callback) {
        callback(null, './uploads/attachments');
        pathFile = file.originalname;
    },
    filename: function (req, file,itemId, cb) {
        cb(null, file.originalname);
        console.log(itemId);

    }
});
var upload = multer({ storage : storage }).single('file');
upload(req,res,function(err) {

    if(err) {
        return res.end("Error uploading file.");
    }

    //save file path in work items doc.attachments
    var path = './uploads/attachments/'+pathFile;

res.end("File is uploaded");
});

已添加服务代码。如何在服务器端获取itemId?

最佳答案

有几种方法可以做到这一点,您可以在帖子选项中设置 params 属性,并传入一个带有 id 的对象作为属性。

    var fd = new FormData();
    var itemId = scope.vm.item._id;

    fd.append('file', scope.files[0]);
    $http.post('http://localhost:8090/file-upload', fd, {
             transformRequest: angular.identity,
                    headers: {'Content-Type': undefined},
                    params: {id: itemId}

                });

关于angularjs - 如何在请求中发送额外数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42137636/

相关文章:

angularjs - 预先填充 angular-ui 的 select2 的问题

javascript - 进程在完成请求之前退出 - AWS Lambdas

node.js - 服务器端 WebKit 与 node.js

node.js - 由于 node-sass 故障,无法安装 laravel-elixir

ios - node-apn 库中出现奇怪的语法错误

angularjs - 动态更改 gridOptions columnDefs

javascript - 在另一个单元格悬停时更改表单元格中的 DIV 类

javascript - AngularJS错误: $injector:unpr Unknown Provider with using ui-router

javascript - angularjs-google-maps - 信息窗口不适用于 ng-repeat

javascript - 如何解压缩(解压缩)NodeJS 请求的模块 gzip 响应正文?