Html5 implements the client to verify the size of the uploaded file (simple instance ),
In HTML 5, you can check the file size and attributes when uploading files on the client. For example, after you select a file, you can check the file size and attributes immediately. This article describes how html5 implements client-side verification of the size of uploaded files. For more information, see.
In HTML 5, you can verify the file upload on the client. For example, after you select a file, you can check the file size and attributes immediately, this is in real time thanks to the new file verification capability on the browser side, which supports HTML 5 browsers, all implement the file API standards implemented by W3C, various information and parameters of the client files can be read.
The example below is as follows:
Copy XML/HTML Code to clipboard
- <Input type = "file" data-file_type = "zip | png" data-max_size = "1000000">
Here, the data-file_type Property specifies the file type, accepts ZIP, PNG files, separated by |, where data-max-size specifies the file size, which is 1 MB. Then use jquery to determine
Copy the content to the clipboard using JavaScript Code
- $ ('Input [type = file] '). each (function ()
- {
- If (typeof $ (this). attr ('data-file_type ') = 'string ')
- {
- Var file_types = $ (this). attr ('data-file_type '). split (' | ');
- }
- Var mimes = get_mimes (file_types );
- // Specify the file size
- Var max_size = parseInt ($ (this). attr ('data-max_size '));
- $ (This). change (function (evt)
- {
- Var finput = $ (this );
- Var files = evt.tar get. files; // get the file object
- Var output = [];
- For (var I = 0, f; f = files [I]; I ++)
- {
- // Check whether the file type meets the specified requirements
- If (jQuery. inArray (f. type, mimes) =-1)
- {
- Alert ('file type' + f. type + 'not allowed ');
- $ (This). val ('');
- Continue;
- }
- // Check the file size
- Else if (f. size> max_size)
- {
- Alert ('maximum file size is '+ max_size + 'bytes .');
- $ (This). val ('');
- }
- // Validation OK
- Else
- {
- Output. push ('[B]', f. name, '[/B] (', f. type | 'n'/A', ')-', f. size, 'bytes, last modified: ', f. lastModifiedDate. toLocaleDateString ());
- }
- }
- Finput. after ('<div>' + output. join ('') + '</div> ');
- });
- });
In the above Code, var mimes = get_mimes (file_types); is actually a method, as follows:
Copy the content to the clipboard using JavaScript Code
- /*
- Get the mimes of a list of extensions as an array
- */
- Function get_mimes (extensions)
- {
- Var mimes = [];
- For (var I in extensions)
- {
- Var ext = extensions [I];
- If (ext in mime_types)
- {
- Var mime = mime_types [ext];
- If ($. isArray (mime ))
- {
- JQuery. merge (mimes, mime );
- }
- Else
- {
- Mimes. push (mime );
- }
- }
- }
- Return mimes;
- }
Here, we will pass in the ZIP and PNG types, and then return the MIME/TYPE corresponding to these types of files, for example, defining a mime_types array, as shown below:
Copy the content to the clipboard using JavaScript Code
- Var mime_types = {
- "Gif": "image \/gif ",
- "Jpeg": ["image \/jpeg", "image \/pjpeg"],
- "Jpg": ["image \/jpeg", "image \/pjpeg"],
- "Jpe": ["image \/jpeg", "image \/pjpeg"],
- "Png": ["image \/png", "image \/x-png"],
- ..................
- }
In HTML 5, the new file API can determine the file type immediately on the client, as shown below:
Copy XML/HTML Code to clipboard
- Var files = evt.tar get. files; // obtain a file object. It is a collection and can contain multiple files.
- Var file_count = files. length; // file length
- Var file_1 = files [0]; // or files. item (0); obtain the first file in multiple files.
- Var name = file_1.name; // get the file name
- Var size = file_1.size; // get the file size
- Var type = file_1.type; // file type
- Var lastModifiedDate = file_1.lastModifiedDate; // file modification time
For details about HTML 5 File Upload, see: http://www.w3.org/TR/file-upload/
The above html5 implementation client verifies the size of the uploaded file (simple example) is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for the customer's house.
Address: http://www.manongjc.com/article/814.html