Jquery Multiple File Upload Name and Size Preview

jQuery MultiFile

The unopinionated mode to implement file selection.

What this is

This jQuery Multiple File Selection Plugin ($.MultiFile) is a non-obstrusive plugin for jQuery that helps users easily select multiple files for upload. Information technology lets you lot utilize some basic restrictions/validation then you lot tin can easily reject files before they're uploaded based on their extension or size before they're uploaded.

What this isn't

This is non a file upload tool. Uploading files require server-side integration and security considerations this project will never support. We tin recommend many, many, many peachy tools if you're looking for a complete upload solution. But if you don't already manage your own server-side integration, or if you take no idea what I'm talking nigh, then this plugin is not for you.

What'southward new?

A fresh start... ish : This projection started a a very long time ago... and information technology'due south been dormant since 2009! Support has been shockingly bad, we admit, but we would like to engage more than actively with our users and the jQuery community from now on. So as of April 2014, we've left Google Code backside and will be starting a fresh project on GitHub. Our other projects (Star Rating & CKEditor will follow before long). We very much encourage and would dearest you lot all to report problems, contribue and discuss this project on GitHub.

Getting started

Just add the multi grade to your file input chemical element.

              <input blazon="file" class="multi"/>            

Utilise the maxlength holding if y'all want to limit the number of files selected.

              <input type="file" class="multi" maxlength="2"/>            

Use the accept belongings if y'all only want files of a certain extension to exist selected.
Separate valid extensions with a "|", similar this: "jpg|gif|png".

              <input type="file" grade="multi" accept="gif|jpg"/>            

NB.: server-side validation is always required

Usage Examples

Using Attributes

The easiest style to get started is to add class="multi" and change the attributes of your element

Instance 1

Use maxlength to limit the number of files selected

                    <input   class="multi"   maxlength="ii" />                  

Limit: 2 files.
Allowed extensions: any.

Case two

Use take to limit the extensions allowed

                    <input   class="multi"   accept="gif|jpg" />                  

Limit: no limit.
Allowed extensions: gif and jpg.

Example 3

Utilise maxlength and accept combined

                    <input   class="multi"   accept="gif|jpg"   maxlength="3" />                  

Limit: 3 files
Allowed extensions: gif, jpg.

Note that server-side validation is always required

Using the class property

There is a style to configure the plugin via the class property, without any knowledge of javascript

Example four
                    <input grade="   multi max-3 "/>                  

Limit: three files.
Allowed extensions: any.

Example five
                    <input class="   multi accept-gif|jpg "/>                  

Limit: no limit.
Allowed extensions: gif, jpg.

Example 6
                    <input course="   multi   max-three   accept-gif|jpg   maxsize-1024 "/>                  

Limit: iii files
Immune extensions: gif, jpg.
Max payload: 1MB (1024 bytes)

Note that server-side validation is ever required

Selecting many files at in one case (HTML5)

And then far, each of the example higher up has merely enabled yous to select one file at a time. To select multiple files at once, just utilise HTML5's multiple aspect (1, 2)

Case 7
                    <input   multiple   class="multi"   maxlength="10" />                  

Limit: 10 files.
Allowed extensions: any.

Example 8
                    <input   multiple   class="multi"   take="gif|jpg|png" />                  

Limit: no limit.
Immune extensions: gif, jpg.

Example 9
                    <input   multiple   grade="multi"   maxlength="10"   accept="gif|jpg|png" />                  

Limit: 10 files
Allowed extensions: gif, jpg, png.

Note that server-side validation is always required

Validating Sizes

It'south mutual to validate the size of the files being selected prior to upload.

Example 10a

Using course maxsize-* (* = max size in kb)

                    <input   multiple   class="multi maxsize-5120" />                  

Limit: any number of files
Immune extensions: any.
Max payload: 5MB

Example 10b

Using data-maxsize attribute (max size in kb)

                    <input   multiple   class="multi"   maxlength="3"   data-maxsize="1024" />                  

Limit: iii files (nether <1MB in total)

Case 10c

Use information-maxfile to validate individual files

                    <input   multiple   class="multi"   maxlength="3"   data-maxfile="1024" />                  

Limit: 3 files (nether <1MB each)

Note that server-side validation is always required

With an Image Preview

There'due south an piece of cake way to add a preview of the image beingness uploaded

Instance 11a

Using class with-preview

                    <input   multiple   class="multi with-preview" />                  

Advanced Examples

Of import: Don't use form="multi" if you utilise any of the examples below this bespeak.

If you utilise class="multi" the plugin will be initialised automatically (taking precedence over your code).

You must invoke the plugin with your ain selector when you use whatsoever of the examples below.

Something like

                    <input type="file" class="this-is-your-course" />                  

.

Followed by

                    // this is your code $(function(){ // wait for page to load      // this is your selector   $('input.this-is-your-class').MultiFile({     // your options go here     max: ii,     accept: 'jpg'   });  });                  

Setting limit via script

                    <input multiple type="file" id="UpTo3Files"/>                  
                    // wait for document to load $(office(){      // up to iii files can exist selected    // invoke plugin   $('#UpTo3Files').MultiFile(5);    // if yous send in a number the plugin   // volition treat it as the file limit  });                  

Endeavor it:

Limit and Extension Filter

                    <input multiple type="file" id="UpTo3Images"/>                  
                    // wait for document to load $(part(){      // up to 3 files can be selected   // only images are allowed    // invoke plugin   $('#UpTo3Images').MultiFile({     max: 3,      have: 'gif|jpg|png'   });  });                  

Try it:

Multi-lingual support

                    <input multiple type="file" id="EmPortugues"/>                  
                    // wait for document to load $(part(){      // utilize a different language   // $file prints the file name   // $ext prints the file extension    // invoke plugin   $('#EmPortugues').MultiFile({      max: 3,      have: 'gif|jpg|png'     STRING: {        remove:'Remover',        selected:'Selecionado: $file',        denied:'Invalido arquivo de tipo $ext!'      }    });  });                  

Endeavor it:

Moving the file list

This example populates the file listing in a custom chemical element

                        $(function() { // wait for document to load    $('#T7').MultiFile({     list: '#T7-list'   }); });                      

This is div#T7-listing - selected files will be populated here...

Customising the file listing

Apply a custom 'remove' image in the file list

                      $(office() { // wait for document to load    $('#T8A').MultiFile({     STRING: {       remove: '<img src="/i/bin.gif" height="xvi" width="sixteen" alt="x"/>'     }   }); });                    

Customising all list content

                    $(function() { // wait for document to load    $('#T8B').MultiFile({     STRING: {       file: '<em title="Click to remove" onclick="$(this).parent().prev().click()">$file</em>',       remove: '<img src="/i/bin.gif" peak="sixteen" width="xvi" alt="ten"/>'     }   }); });                  

Using events

The arguments passed on to each upshot handler are:
element: file element which triggered the event
value: the value of the chemical element in question
master_element: the original element containing all relevant settings

Selection events:

  • onFileAppend
  • afterFileAppend
  • onFileSelect
  • afterFileSelect
  • onFileRemove
  • afterFileRemove

Validation events:

  • onFileDuplicate
  • onFileInvalid
  • onFileTooMany
  • onFileTooBig
  • onFileTooMuch
                          <input multiple type="file" id="WithEvents"/>                        
                          // await for document to load $(function(){      // 2 jpgs under 100kb only    $('#WithEvents').MultiFile({     max: 2,     max_size: 100,     have: 'jpg',     onFileRemove: function(element, value, master_element) {       $('#F9-Log').append('<li>onFileRemove - ' + value + '</li>')     },     afterFileRemove: function(element, value, master_element) {       $('#F9-Log').suspend('<li>afterFileRemove - ' + value + '</li>')     },     onFileAppend: function(chemical element, value, master_element) {       $('#F9-Log').append('<li>onFileAppend - ' + value + '</li>')     },     afterFileAppend: function(element, value, master_element) {       $('#F9-Log').append('<li>afterFileAppend - ' + value + '</li>')     },     onFileSelect: function(element, value, master_element) {       $('#F9-Log').append('<li>onFileSelect - ' + value + '</li>')     },     afterFileSelect: function(element, value, master_element) {       $('#F9-Log').append('<li>afterFileSelect - ' + value + '</li>')     },     onFileInvalid: function(element, value, master_element) {       $('#F9-Log').append('<li>onFileInvalid - ' + value + '</li>')     },     onFileDuplicate: function(element, value, master_element) {       $('#F9-Log').suspend('<li>onFileDuplicate - ' + value + '</li>')     },     onFileTooMany: part(element, value, master_element) {       $('#F9-Log').suspend('<li>onFileTooMany - ' + value + '</li>')     },     onFileTooBig: function(element, value, master_element) {       $('#F9-Log').append('<li>onFileTooBig - ' + value + '</li>')     },     onFileTooMuch: function(chemical element, value, master_element) {       $('#F9-Log').append('<li>onFileTooMuch - ' + value + '</li>')     }   }); });                        

This is div#F9-Log - selected files volition be populated here...

Multi-lingual support

The plugin doesn't have any additional languages congenital-in, but it's very piece of cake to customise the messages to any language of your choosing. See the examples below...

NB.: This example has been configured to accept gif/pg files only in order to demonstrate the fault messages.

Method ane: Using class property (requires the MetaData plugin)

            <input type="file"   class="multi {     take:'gif|jpg',     max:3,     STRING:{        remove:'Remover',        selected:'Selecionado: $file',        denied:'Invalido arquivo de tipo $ext!',        indistinguishable:'Arquivo ja selecionado:\n$file!'      }    }" />          

Method two: Programatically by ID (ONE element just, does not require MetaData plugin)

            <input type="file" id="PortugueseFileUpload" />          
            $(role(){   $('#PortugueseFileUpload').MultiFile({     accept:'gif|jpg',      max:three,      STRING: {       remove:'Remover',         selected:'Selecionado: $file',        denied:'Invalido arquivo de tipo $ext!',        duplicate:'Arquivo ja selecionado:\n$file!'     }   }); });          

Method 3: Programatically ( north emlements, does not require MetaData plugin)

See this feature asking for details

            <input type="file" form="multi-pt" /> <input type="file" class="multi-pt" /> <input type="file" form="multi-pt" />          
            $(role(){   $('.multi-pt').MultiFile({     have:'gif|jpg',      max:three,      STRING: {        remove:'Remover',        selected:'Selecionado: $file',        denied:'Invalido arquivo de tipo $ext!',        duplicate:'Arquivo ja selecionado:\n$file!'     }   }); });          

Installation

Requirements

Y'all'll need jQuery, we recommend using Google Hosted Libraries.

            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript" language="javascript"></script>          

The plugin

Either download the latest version and host information technology on your own server

            <script src="path/to/your/jquery.MultiFile.js" type="text/javascript" language="javascript"></script>          

OR if you lot like living on the border, hotlink the latest release straight from GitHub

            <script src="//github.com/fyneworks/multifile/blob/master/jQuery.MultiFile.min.js" blazon="text/javascript" linguistic communication="javascript"></script>          

Support

We very much encourage and would love y'all all to report issues, contribue and discuss this project on GitHub.

There's besides a README.doctor for quick reference, if you're that way inclined.

This project started a a very long fourth dimension ago... and information technology's been dormant since 2009! Support has been shockingly bad, we admit, only we would like to engage more actively with our users and the jQuery community from at present on. And then equally of April 2014, we've left Google Lawmaking backside and will be starting a fresh project on GitHub. Our other projects (Star Rating & CKEditor will follow soon).

Credit where it'southward due!

  • Fyneworks.com professional web design and google SEO experts
  • Dean Edwards - Writer of JS Packer used to compress the plugin
  • Adrian Wróbel - Fixed a nasty bug then the script could work perfectly in Opera
  • Jonas Wagner - Modified plugin and so newly created elements are an exact copy of the original element (ie.: persists attributes)
  • Mike Alsup - Writer of several jQuery Plugins...
    • Suggested solution to chemical element naming convention / server-side handling
    • Form plugin - Used to submit forms via ajax
    • blockUI plugin - Used to show pretty fault messages
  • Julien Phalip - Identified conflict with variable name 'course' in several methods

License Info

Multiple File Selection Plugin by Fyneworks.com is licensed, as jQuery is, under the MIT License. Creative Commons License

starksmadentopere.blogspot.com

Source: http://lampspw.wallonie.be/dgo4/tinymvc/myfiles/plugins/multifile-2.2.1/docs.html

0 Response to "Jquery Multiple File Upload Name and Size Preview"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel