Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. <form enctype="multipart/form-data" ng-controller="view1Ctrl">
  2. <input type="file" multiple ng-file-model="myFile"/>
  3. <input type="text" ng-model="desc" placeholder="Enter desc" />
  4. <input type="text" ng-model="title" placeholder="Enter title" />
  5. <button type="submit" class="btn btn-primary"
  6. ng- click="s()">Submit</button>
  7. </form>
  8.  
  9. app.directive('ngFileModel', ['$parse', function ($parse) {
  10. return {
  11. restrict: 'A',
  12. link: function (scope, element, attrs) {
  13. var model = $parse(attrs.ngFileModel);
  14. var isMultiple = attrs.multiple;
  15. var modelSetter = model.assign;
  16. element.bind('change', function () {
  17. var values = [];
  18. angular.forEach(element[0].files, function (item) {
  19. var value = {
  20. // File Name
  21. name: item.name,
  22. //File Size
  23. size: item.size,
  24. //File URL to view
  25. url: URL.createObjectURL(item),
  26. // File Input Value
  27. _file: item
  28. };
  29. values.push(value);
  30. });
  31. scope.$apply(function () {
  32. if (isMultiple) {
  33. modelSetter(scope, values);
  34. } else {
  35. modelSetter(scope, values[0]);
  36. }
  37. });
  38. });
  39. }
  40. };
  41. }]);
  42.  
  43. app.controller('view1Ctrl', function($scope, $http) {
  44. $scope.s = function() {
  45. var file = $scope.myFile;
  46. var payload = new FormData();
  47. console.dir(file);
  48. console.dir($scope.myFile);
  49. payload.append('file', $scope.myFile);
  50. payload.append('desc', $scope.desc)
  51.  
  52. $http.post('/view1', payload, {
  53. transformRequest: angular.identity,
  54. headers: {'Content-Type': undefined}
  55. }).success(function(data) {
  56. console.log("posted successfully");
  57. }).error(function(data) {
  58. console.error("error in posting");
  59. });
  60. $http.post('/view1',$scope.formData).
  61. success(function(data) {
  62. console.log("posted successfully");
  63. }).error(function(data) {
  64. console.error("error in posting");
  65. })
  66. }
  67. });
  68.  
  69. app.post('/view1', function(req, res) {
  70. // fs.readFile(req.files.file.path, function(err, data){
  71. // Do something with the data (which holds the file information)
  72. // });
  73. //console.log("desc "+ req.files.file);
  74. console.log("desc "+ req.body.desc);
  75. console.log("title "+req.body.title);
  76. res.end();
  77. });`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement