找到了一种将文件从angular上传到我的节点服务器以便由multer处理的好方法。图像上传和处理工作完美,但我想为此请求附加一个键/值对。不幸的是,我不知道如何将键值对与此表单帖子一起附加到此请求中!
var files = document.getElementById("change-pic-input").files;
var fd = new FormData();
fd.append("file", files[0])
uploadProfilePic(fd);
});
var uploadProfilePic = function(fd) {
$http.post("/api/spot/pic/post", fd,{
headers: {'Content-Type': undefined},
transformRequest: angular.identity
}).then(
function successCallback(response) {
console.log("Success post image")
}, function errorCallback(response) {
console.log("fail post image")
});
};
很想听听$ http专家或比我聪明的人来解决这个问题。
答案 0 :(得分:0)
我认为您可以将另一个数据附加到formData变量中。如下;
fd.append('key', value);
或者如果你想发送带有该请求的json数据。
fd.append('jsonData', { key: value });
对于后端方面;
据我所知,您使用req.files来访问上传的文件。现在,我认为您可以使用req.body
来覆盖请求中的其他数据。喜欢; req.body.jsonData
或req.body.key
。
答案 1 :(得分:0)
我找到的最基本的解决方案是将参数附加到URL,如
"/api/spot/pic/post?key=value"
然后在服务器端
value = req.query.key
如果可能的话,我希望有更好的解决方案。感谢。