Home >Web Front-end >HTML Tutorial >Mobile terminal HTML5 realizes file upload_HTML/Xhtml_Web page production

Mobile terminal HTML5 realizes file upload_HTML/Xhtml_Web page production

WBOY
WBOYOriginal
2016-05-16 16:36:061376browse

Most files uploaded on the PC side use plug-ins. It doesn’t matter if you introduce flash. However, if the mobile side still uses various redundant plug-ins, it will probably be sprayed to death. The project needs to have the function of uploading images, since H5 already has relevant interfaces. And the compatibility is good, of course, priority is given to using H5 for implementation.

The main technologies used are:

ajax

FileReader

FormData

HTML structure:

XML/HTML CodeCopy content to clipboard
  1. <div class="camera-area"> 
  2.  <form enctype="multipart/ form-data" method="post">
  3.  <input type="file" name="fileToUpload" class="fileToUpload" accept="image/*" capture="camera"/>
  4.  <div class="upload- progress"><span>span>div>
  5. form>
  6.  <div class="thumb" >div>
  7. div>

The packaged upload.js depends on zepto

JavaScript CodeCopy content to clipboard
  1. (function($) {   
  2.   $.extend($.fn, {   
  3.     fileUpload: function(opts) {   
  4.       this.each(function() {   
  5.         var $self = $(this);   
  6.         var doms = {   
  7.           "fileToUpload": $self.find(".fileToUpload"),   
  8.           "thumb": $self.find(".thumb"),   
  9.           "progress": $self.find(".upload-progress")   
  10.         };   
  11.         var funs = {   
  12.           //选择文件,获取文件大小,也可以在这里获取文件格式,限制用户上传非要求格式的文件   
  13.           "fileSelected"function() {   
  14.             var files = (doms.fileToUpload)[0].files;   
  15.             var count = files.length;   
  16.             for (var index = 0; index < count; index ) {   
  17.               var file = files[index];   
  18.               var fileSize = 0;   
  19.               if (file.size > 1024 * 1024)   
  20.                 fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString()   'MB';   
  21.               else  
  22.                 fileSize = (Math.round(file.size * 100 / 1024) / 100).toString()   'KB';   
  23.             }
  24. funs.uploadFile();
  25. },
  26.  //Upload files asynchronously
  27. uploadFile: function() {
  28.  var fd = new FormData();//Create form data object
  29. var files = (doms.fileToUpload)[0].files;
  30. var count = files.length;
  31.  for (var index = 0; index < count; index ) {
  32. var file = files[index];
  33. fd.append(opts.file, file);//Add the file to the form data
  34. funs.previewImage(file);//Preview the image before uploading, or preview the txt through other methods
  35.                                                            
  36. var xhr = new XMLHttpRequest();
  37. xhr.upload.addEventListener(
  38. "progress", funs.uploadProgress, false); // Monitor upload progress
  39. xhr.addEventListener(
  40. "load", funs.uploadComplete, false);
  41. xhr.addEventListener(
  42. "error", opts.uploadFailed, false);
  43. xhr.open(
  44. "POST", opts.url);
  45. xhr.send(fd);
  46. },
  47. //File preview
  48. previewImage: function(file) {
  49. var gallery = doms.thumb;
  50. var img = document.createElement("img");
  51. img.file = file;
  52. doms.thumb.html(img);
  53.  //Use the FileReader method to display the image content
  54. var reader = new FileReader();
  55. reader.onload = (function(aImg) {
  56. return function(e) {
  57. aImg.src = e.target.result;
  58.                                              
  59. })(img);
  60. reader.readAsDataURL(file);
  61. },
  62. uploadProgress:
  63. function(evt) {
  64. if (evt.lengthComputable) {
  65. var percentComplete = Math.round(evt.loaded * 100 / evt.total);
  66. doms.progress.html(percentComplete.toString()
  67. '%');
  68.                                                            
  69. },
  70. "uploadComplete"
  71. : function(evt) { alert(evt.target.responseText)
  72.                                                                   
  73. };
  74. doms.fileToUpload.on("change", function() {
  75. doms.progress.find("span").width("0");
  76. funs.fileSelected();
  77. });
  78. });
  79. }
  80. });
  81. })(Zepto);

Calling method:

JavaScript CodeCopy content to clipboard
  1. $(".camera-area").fileUpload({
  2.  "url": "savetofile.php"
  3.  "file": "myFile"
  4. });

PHP part:

PHP CodeCopy content to clipboard
  1. if (isset($_FILES['myFile'])) {
  2. // Example:
  3. writeLog($_FILES);
  4. move_uploaded_file($_FILES['myFile']['tmp_name'], "uploads/" . $_FILES['myFile']['name']);
  5. echo 'successful';
  6. }
  7. function writeLog($log){
  8.  if(is_array($log) | | is_object($log)){
  9. $log = json_encode($log);
  10. }
  11.  $log = $log."rn";
  12.  file_put_contents('log.log', $log,FILE_APPEND);
  13. }
  14. ?>

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Original text: http://www.cnblogs.com/hutuzhu/p/5254532.html

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn