This article mainly introduces the materials related to file upload through HTML5 on the Mobile End, and uses H5 to Easily upload files. If you are interested, refer to the plug-ins used to upload files on the PC end, it does not matter if flash is introduced. However, if the Mobile End still needs to be sprayed with redundant plug-ins, the project needs to upload images, since H5 already has related interfaces and is compatible with each other, H5 is preferred.
The main technologies used are:
Ajax
FileReader
FormData
HTML structure:
Copy XML/HTML Code to clipboard
-
-
-
-
-
-
-
The encapsulated upload. js depends on zepto.
Copy the content to the clipboard using JavaScript Code
- (Function ($ ){
- $. Extend ($. fn ,{
- FileUpload: function (opts ){
- This. each (function (){
- Var $ self = $ (this );
- Var doms = {
- "FileToUpload": $ self. find (". fileToUpload "),
- "Thumb": $ self. find (". thumb "),
- "Progress": $ self. find (". upload-progress ")
- };
- Var funs = {
- // Select a file to obtain the file size. You can also obtain the file format here to restrict users from uploading non-required files.
- "FileSelected": function (){
- Var files = (doms. fileToUpload) [0]. files;
- Var count = files. length;
- For (var index = 0; index <count; index ++ ){
- Var file = files [index];
- Var fileSize = 0;
- If (file. size & gt; 1024*1024)
- FileSize = (Math. round (file. size * 100/(1024*1024)/100). toString () + 'mb ';
- Else
- FileSize = (Math. round (file. size * 100/1024)/100). toString () + 'kb ';
- }
- Funs. uploadFile ();
- },
- // Uploads files asynchronously.
- UploadFile: function (){
- Var fd = new FormData (); // create a form data object
- Var files = (doms. fileToUpload) [0]. files;
- Var count = files. length;
- For (var index = 0; index <count; index ++ ){
- Var file = files [index];
- Fd. append (opts. file, file); // Add the file to the form data.
- Funs. previewImage (file); // preview the image before upload. You can also preview the image by using other methods.
- }
- Var xhr = new XMLHttpRequest ();
- Xhr. upload. addEventListener ("progress", funs. uploadProgress, false); // listen to the upload progress
- Xhr. addEventListener ("load", funs. uploadComplete, false );
- Xhr. addEventListener ("error", opts. uploadFailed, false );
- Xhr. open ("POST", opts. url );
- Xhr. send (fd );
- },
- // File Preview
- PreviewImage: function (file ){
- Var gallery = doms. thumb;
- Var img = document. createElement ("img ");
- Img. file = file;
- Doms.thumb.html (img );
- // Use the FileReader method to display the image content
- Var reader = new FileReader ();
- Reader. onload = (function (aImg ){
- Return function (e ){
- AImg. src = e.tar get. result;
- };
- }) (Img );
- Reader. readAsDataURL (file );
- },
- UploadProgress: function (evt ){
- If (evt. lengthComputable ){
- Var percentComplete = Math. round (evt. loaded * 100/evt. total );
- Doms.progress.html (percentComplete. toString () + '% ');
- }
- },
- "UploadComplete": function (evt ){
- Alert(evt.tar get. responseText)
- }
- };
- Doms. fileToUpload. on ("change", function (){
- Doms. progress. find ("span"). width ("0 ");
- Funs. fileSelected ();
- });
- });
- }
- });
- }) (Zepto );
Call method:
Copy the content to the clipboard using JavaScript Code
- $ (". Camera-area"). fileUpload ({
- "Url": "savetofile. php ",
- "File": "myFile"
- });
PHP section:
Copy content from PHP Code to clipboard
-
- If (isset ($ _ FILES ['myfile']) {
- // Example:
- WriteLog ($ _ FILES );
- Move_uploaded_file ($ _ FILES ['myfile'] ['tmp _ name'], "uploads/". $ _ FILES ['myfile'] ['name']);
- Echo 'successful ';
- }
- Function writeLog ($ log ){
- If (is_array ($ log) | is_object ($ log )){
- $ Log = json_encode ($ log );
- }
- $ Log = $ log. "\ r \ n ";
-
- File_put_contents ('Log. log', $ log, FILE_APPEND );
- }
- ?>
The above is all the content of this article, hoping to help you learn.
Original article: http://www.cnblogs.com/hutuzhu/p/5254532.html