【问题标题】:Http request is different between client and server客户端和服务器之间的Http请求不同
【发布时间】:2015-08-02 13:28:49
【问题描述】:

在客户端,我使用 $http 发送请求:

Upload.upload({
       url: 'http://localhost:3000/upload',
       fields: {
            'username': $scope.username
       },
       file: file
})

在服务器端,这是路由:

app.route('/upload')
    .get(function (req) {
        for (var key in req)
            console.log(key);
})

但是我在 req 中看不到任何键名文件?两者之间的差异是什么原因?

【问题讨论】:

  • app 当前使用的是什么中间件?任何正文解析器?

标签: angularjs node.js http


【解决方案1】:

使用 post 上传文件需要添加 header,需要进行 multipart/form-data 请求,

   var fd = new FormData();
     fd.append("file", file);

    $http.post(uploadUrl, fd, {
        withCredentials: true,
        headers: {'Content-Type': undefined },
        transformRequest: angular.identity
    })

more on this answer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 2013-03-07
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 2020-07-15
    相关资源
    最近更新 更多