decentralized-blogging-platform/webapp/views/newmodel/newmodel.js
webapp/views/newmodel/newmodel.jsRaw
1'use strict';
2
3angular.module('app.newmodel', ['ngRoute'])
4
5 .config(['$routeProvider', function($routeProvider) {
6 $routeProvider.when('/newmodel', {
7 templateUrl: 'views/newmodel/newmodel.html',
8 controller: 'NewModelCtrl'
9 });
10 }])
11
12 .controller('NewModelCtrl', function($scope, $rootScope, $http, toastr) {
13
14 $scope.file = {};
15
16 $scope.upload = function() {
17 console.log("upload model");
18 var formdata = new FormData();
19 formdata.append("file", $scope.file);
20
21 //add the file to ipfs
22 /*$http({
23 url: ipfs_url + 'add',
24 method: "POST",
25 headers: {
26 "Content-Type": undefined
27 },
28 data: formdata
29 })
30 .then(function(data) {
31 console.log("data: ");
32 console.log(data.data);
33 toastr.success("Model added to IPFS");
34 },
35 function(data) {
36 console.log(data);
37 toastr.error("Error adding Model to IPFS");
38 });*/
39
40 //add the data to userdata
41 $http({
42 url: clienturl + 'model',
43 method: "POST",
44 headers: {
45 "Content-Type": undefined
46 },
47 data: $scope.model
48 })
49 .then(function(data) {
50 console.log("data: ");
51 console.log(data.data);
52 window.location="/";
53 toastr.success("Model uploaded");
54 },
55 function(data) {
56 console.log(data);
57 });
58
59
60 };
61
62
63
64 })
65 .directive('fileModel', ['$parse', function($parse) {
66 //directive code from https://www.tutorialspoint.com/angularjs/angularjs_upload_file.htm
67 return {
68 restrict: 'A',
69 link: function(scope, element, attrs) {
70 var model = $parse(attrs.fileModel);
71 var modelSetter = model.assign;
72 element.bind('change', function() {
73 scope.$apply(function() {
74 modelSetter(scope, element[0].files[0]);
75 });
76 });
77 }
78 };
79 }]);