我在console.log(file);
中获取了该文件,但我不知道如何传递Webservice以及我应该编写哪些代码来上传我的文件?
app.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function () {
scope.$apply(function () {
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
app.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function (file, uploadUrl) {
debugger;
var fd = new FormData();
fd.append('file', file);
//alert();
debugger;
console.log(fd);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
})
.success(function () {
})
.error(function () {
});
}
}]);
$scope.uploadFile = function () { // This Function will Call when Click on upload button after selecting a file.
var file = $scope.myFile;
console.log(file);
var uploadUrl = "http://localhost:25204/AdminOperation.asmx/Upload", //Url of webservice/api/server
promise = fileUpload.uploadFileToUrl(file, uploadUrl);
promise.then(function (response) {
$scope.serverResponse = response;
}, function () {
$scope.serverResponse = 'An error has occurred';
})
};