I want the best way to upload files with loading image in AngularJS. At the same time i want to limit the size to 10MB.
Please give the best way to achieve this?
See this example you get some idea
HTML CODE :
<form class="upload-form">
<input class="upload-file" data-max-size="2048" type="file" >
<input type=submit>
</form>
SCRIPT:
$(function(){
var fileInput = $('.upload-file');
var maxSize = fileInput.data('max-size');
$('.upload-form').submit(function(e){
if(fileInput.get(0).files.length){
var fileSize = fileInput.get(0).files[0].size; // in bytes
if(fileSize>maxSize){
alert('file size is more than ' + maxSize + ' bytes');
return false;
}else{
alert('file size is correct - '+fileSize+' bytes');
}
}else{
alert('Please select the file to upload');
return false;
}
});
});
its already in jsfiddle
While this question is somewhat old, I still think it's worthwhile to provide the best possible answer for those seeking it. The answer above relies on jQuery for the superficial aspects, but let's use a style fitting for Angular development.
Here I reference the actual library author's own recommendation when asked the same question, altering it for the size asked in the question above:
uploader = new FileUploader();
//...
uploader.filters.push({
'name': 'enforceMaxFileSize',
'fn': function (item) {
return item.size <= 10485760; // 10 MiB to bytes
}
});
EXAMPLE UPDATED ABOVE: to reflect changes to the angular-file-upload
API.
Note that this relies on the file's size
attribute (Blob size in bytes), which is supported only by modern browsers (namely IE10 and up).
You can use this lib:
https://github.com/danialfarid/ng-file-upload
ngf-min-size='10' // minimum acceptable file size in bytes
ngf-max-size='1000' // maximum acceptable file size in bytes