How to show something on frontend (Angular) when node app crashed - angularjs

I have developed an application in mean stack and there are some processes which took extra time to perform such as inserting thousands of data into mongodb.
It crashes sometime and the UI of the application left hanging in the middle.
Is there any way to overcome this and show some error message to user when app crashes.
Below is some code example :-
Upload.upload({
url: '/api/uploadarchive',
data: {file: file}
}).then(function (resp) {
$scope.progress = false;
console.log('Success ');
}, function (resp) {
console.log('Error status: ' + resp.status);
}, function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
//console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
console.log('progress: ' + progressPercentage + '% ');
});

I'd recommend using $http interceptors. Interceptors will help you to handle errors in a generic way so you will not have to use error callback for each request separately. For example you can use responseError interceptor to check if response status of any request is 404 and in such case redirect to an error page:
define factory with your interceptors:
yourAppModule.factory('myHttpInterceptor', function($q, $state, dependency2) {
return {
'responseError': function(rejection) {
var status = rejection.status;
if (status === 404) {
$state.go('errorPage');
}
return $q.reject(rejection);
}
};
});
and then register it inside your application config:
$httpProvider.interceptors.push('myHttpInterceptor');

Related

Making second Request after first request with angular ng-file-upload

I'm trying to upload two files by making one request for the first first and after I do the second by sending an second file.But it seems that the second request doesn't seems to be completing. I'm using ng-file-upload angular directive and the two files are being recognized by my model.
$scope.send = function(files) {
Upload.upload({
url: '/promotion/createPromotion',
data: { file: files.file1, name: files.name, description: files.description, startDate: files.start_date, endDate: files.end_date }
}).success(function(resp) {
console.log(resp);
var idVar = resp.id;
console.log(idVar);
Upload.upload({
url: '/promotion/createImage',
data: { files: files.file2, id: idVar }
}).then(function(resp) {
console.log(resp.id);
}, function(resp) {
});
}, function(resp) {
console.log('Error status: ' + resp.status);
}, function(evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
// console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
});
};
Update
after a little try, i found that the three record were created successfully but in the four seems that the nested upload function triggered first and that's why is giving me an exception but even so; i don't know how to make it work properly

show success page if form is successfully submitted

How do i track if a upload is success and redirect to a certain page using angular and ng-file upload (https://github.com/danialfarid/ng-file-upload)
Currently it uploads the file but i need to show an error if the form is failed to insert to mysql or show the success page
Below is my angular function
Upload.upload({
url: 'api/upload-image.php',
method: 'POST',
file: file,
data: {
'awesomeThings': $scope.awesomeThings,
'targetPath' : '/media/'
}
})
in my php code
$status = $this->Upload_tools->add($data);
If an upload is access status will return true or else false
if success i want to show this page
$location.path('/show/'+qid);
See example code in ng-file-upload read.me:
$scope.upload = function (file) {
Upload.upload({
url: 'upload/url',
data: {file: file, 'username': $scope.username}
}).then(function (resp) {
console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
}, function (resp) {
console.log('Error status: ' + resp.status);
}, function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
});
Now, your code will be something like this:
$scope.upload = function (file) {
Upload.upload({
url: 'api/upload-image.php',
method: 'POST',
file: file,
data: {
'awesomeThings': $scope.awesomeThings,
'targetPath' : '/media/'
}
}).then(function (resp) {
//Success case code
$location.path('/show/'+qid);
}, function (resp) {
//Error case code
}, function (evt) {
//progress calculation
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
});

files are not storing in local using ngfileupload

I am trying to upload files using Node Api on server and i m using ngfileupload angular Module to handle image on front end.
below is my code:
app.controller('MyCtrl',['Upload','$window',function(Upload,$window){
var vm = this;
vm.submit = function(){
if (vm.upload_form.file.$valid && vm.file) {
vm.upload(vm.file);
}
}
vm.upload = function (file) {
Upload.upload({
url: '/api/upload',
data:{file:file} model
}).then(function (resp) {
if(resp.data.error_code === 0){ //validate success
$window.alert('Success ' + resp.config.data.file.name + 'uploaded. Response: ');
} else {
$window.alert('an error occured');
}
}, function (resp) {
console.log('Error status: ' + resp.status);
$window.alert('Error status: ' + resp.status);
}, function (evt) {
console.log(evt);
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
vm.progress = 'progress: ' + progressPercentage + '% ';
});
};
}]);
and my node api code is
apiRoutes.put('/upload', function(req, res){
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function(fieldname,file,filename){
var filePath=path.join(__dirname, './public/file', filename);
fstream=fs.createWriteStream(filePath);
file.pipe(fstream);
fstream.on('close', function(){
console.log("File Saved...........");
});
});
});
Now problem is that , when i hit upload button its showing an alert Error Status:404 and an error:
Not allowed to load local resource: file:///C:/fakepath/IMG-20160126-WA0013.jpg
I dont know how to fix it...
please help me
NgFileUpload send POST request by default. Your api router accepts PUT requests only.
Try to change apiRoutes.put... to apiRouter.post... or set
Upload.upload({
url: '/api/upload',
data: {file:file},
method: 'PUT'
})

ng-file-upload returns 401

I'm using ng-file-upload to upload PDF to a Spring MVC back server on a different port. I have another requests made with normal $http, but when I use the Upload object I always get error 401. I can see on the browser's network tab that Cookie header is not send, but in the other cases it does.
Here is my code:
Upload.upload({
url: $http.defaults.backUrl + '/oficio/upload',
data: {file: file, 'username': 'Alex'},
withCredentials: true
}).then(function (resp) {
console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
}, function (resp) {
console.log('Error status: ' + resp.status);
}, function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
});
The request goes through my CORS filter, I'm sure because it sets the right headers, but the request doesn't send the Cookie header:
And this is the response:
When use $http, I don't get that error, this is a sample request with $http:
$http({
method: 'POST',
url: $http.defaults.backUrl + '/oficio/upload',
data: data,
headers: {
'Content-Type': undefined
}
});
When $http I can get the file, but canĀ“t track the progress.
Any help will be appreciated.

file upload in sails

i want to know, how to use upload files code in sails using angular upload
this is code for angular
var app = angular.module('app', ['angularFileUpload']);
app.controller('ImageCtrl', ['$scope', '$upload', function ($scop
e, $upload) {
$scope.$watch('files', function () {
$scope.upload($scope.files);
});
$scope.upload = function (files) {
window.alert(JSON.stringify(files));
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
$upload.upload({
url: '???????',
fields: {
'username': $scope.username
},
file: file
}).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' +
evt.config.file.name);
}).success(function (data, status, headers, config) {
console.log('file ' + config.file.name + 'uploaded. Response: ' +
JSON.stringify(data));
});
}
}
};
}]);
in above code , what i should give url ? , my requirement is, i want to upload files in locally using sails mongodb.
Any one please share the server side nodejs code..
Sails have built-in streaming file uploads utility called skipper. In general you can upload files from angular to any sails endpoint. Then you just need to perform something like this in your controller:
req.file('avatar').upload(function (err, uploadedFiles){
if (err) return res.send(500, err);
return res.send(200, uploadedFiles);
});
You can also pass skipper options:
req.file('avatar').upload({
// ...any other options here...
}, ...);
If you aplication should upload files frequently I would recommend you to create service for this based on skipper API.

Resources