Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I limit the size of attachment (file upload) in AngularJS?

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?

like image 247
prince Avatar asked Feb 24 '14 10:02

prince


People also ask

How can I limit the size of attachment file upload in angular?

You can restrict the maximum allowed file size (in bytes) by using the maxFileSize property. If the selected file exceeds the maximum size, an error message will be displayed.

How do I change max upload file size?

Open the file in any text editor and add the following code. @ini_set( 'upload_max_size' , '20M' ); @ini_set( 'post_max_size', '13M'); @ini_set( 'memory_limit', '15M' ); Save your changes, and it should increase your file upload size.


1 Answers

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).

like image 156
runderworld Avatar answered Oct 12 '22 01:10

runderworld