Angularjs和Nodejs express
我试图通过angularjs(客户端)和Nodejs express(服务器端)上传图像。我宁愿不使用表单,因为我的公司不使用表单 这是我控制器的一部分-Angularjs和Nodejs express,express,Express,我试图通过angularjs(客户端)和Nodejs express(服务器端)上传图像。我宁愿不使用表单,因为我的公司不使用表单 这是我控制器的一部分- $scope.uploadPhotoToServer = function () { console.log('will upload to album ' + this.albumName + ' file ' + this.userPhoto); var fd = new FormData(); //Take
$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
});
}]);
我希望你能向我解释一下这是如何工作的,以及我如何能继续下去
提前感谢:)我不确定您想知道什么,或者您不了解什么部分,但这基本上只是创建了一个$scope成员函数,将userPhoto和albumName添加到表示表单的FormData对象(fd)中。该函数然后使用$http将该对象发送到服务器。您可以将$http看作是一个典型的ajax命令,它只向服务器发送数据。(它实际上使用与ajax相同的XMLhttpRequest对象 如果需要,这里是对正在使用的FormData()的引用: