Guest User

Untitled

a guest
Dec 12th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. app.directive('fileModel', ['$parse', function ($parse) {
  2. return {
  3. restrict: 'A',
  4. link: function (scope, element, attrs) {
  5. var model = $parse(attrs.fileModel);
  6. var modelSetter = model.assign;
  7.  
  8. element.bind('change', function () {
  9. scope.$apply(function () {
  10. modelSetter(scope, element[0].files[0]);
  11. });
  12. });
  13. }
  14. };
  15. }]);
  16.  
  17. app.service('fileUpload', ['$http', function ($http) {
  18. this.uploadFileToUrl = function (file, uploadUrl) {
  19. debugger;
  20. var fd = new FormData();
  21. fd.append('file', file);
  22. //alert();
  23. debugger;
  24. console.log(fd);
  25. $http.post(uploadUrl, fd, {
  26. transformRequest: angular.identity,
  27. headers: { 'Content-Type': undefined }
  28. })
  29. .success(function () {
  30. })
  31.  
  32. .error(function () {
  33. });
  34. }
  35. }]);
  36.  
  37.  
  38. $scope.uploadFile = function () { // This Function will Call when Click on upload button after selecting a file.
  39. var file = $scope.myFile;
  40. console.log(file);
  41. var uploadUrl = "http://localhost:25204/AdminOperation.asmx/Upload", //Url of webservice/api/server
  42. promise = fileUpload.uploadFileToUrl(file, uploadUrl);
  43. promise.then(function (response) {
  44. $scope.serverResponse = response;
  45. }, function () {
  46. $scope.serverResponse = 'An error has occurred';
  47. })
  48. };
Add Comment
Please, Sign In to add comment