我试图通过angularjs(客户端)和Nodejs express(服务器端)上传图像。我不愿意使用表格,因为我的公司不使用它。
这是我的控制器的一部分 -
$scope.uploadPhotoToServer = function () {
console.log('will upload to album ' + this.albumName + ' file ' + this.userPhoto);
var fd = new FormData();
//Take the first selected file
fd.append("file", this.userPhoto);
fd.append("album", this.albumName);
$http.post('/upload', fd, {
withCredentials: true,
headers: {'Content-Type': undefined },
transformRequest: angular.identity
});
}]);
我希望你能解释一下这是如何运作的以及我如何继续下去 在此先感谢:)
答案 0 :(得分:0)
我不确定你想知道什么,或者你不了解哪个部分,但这基本上只是创建了一个$ scope成员函数,它将userPhoto和albumName添加到FormData对象(fd) ,表示form.The函数然后使用$ http将该对象发送到服务器。您可以将$ http视为仅将数据发送到服务器的典型ajax命令。 (它实际上使用的是与ajax相同的XMLhttpRequest对象。
如果需要,这里是对正在使用的FormData()的引用: https://developer.mozilla.org/en-US/docs/Web/API/FormData