PC端上傳文件多半用插件,引入flash都沒關(guān)系,但是移動端要是還用各種冗余的插件估計(jì)得被噴死,項(xiàng)目里面需要做圖片上傳的功能,既然H5已經(jīng)有相關(guān)的接口且兼容性良好,當(dāng)然優(yōu)先考慮用H5來實(shí)現(xiàn)。
用的技術(shù)主要是:
ajax
FileReader
FormData
HTML結(jié)構(gòu):
XML/HTML Code
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>
8.
已經(jīng)封裝好的upload.js,依賴zepto
JavaScript Code
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. //異步上傳文件
27. uploadFile: function() {
28. var fd = new FormData();//創(chuàng)建表單數(shù)據(jù)對象
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);//將文件添加到表單數(shù)據(jù)中
34. funs.previewImage(file);//上傳前預(yù)覽圖片,也可以通過其他方法預(yù)覽txt
35. }
36. var xhr = new XMLHttpRequest();
37. xhr.upload.addEventListener("progress", funs.uploadProgress, false);//監(jiān)聽上傳進(jìn)度
38. xhr.addEventListener("load", funs.uploadComplete, false);
39. xhr.addEventListener("error", opts.uploadFailed, false);
40. xhr.open("POST", opts.url);
41. xhr.send(fd);
42. },
43. //文件預(yù)覽
44. previewImage: function(file) {
45. var gallery = doms.thumb;
46. var img = document.createElement("img");
47. img.file = file;
48. doms.thumb.html(img);
49. // 使用FileReader方法顯示圖片內(nèi)容
50. var reader = new FileReader();
51. reader.onload = (function(aImg) {
52. return function(e) {
53. aImg.src = e.target.result;
54. };
55. })(img);
56. reader.readAsDataURL(file);
57. },
58. uploadProgress: function(evt) {
59. if (evt.lengthComputable) {
60. var percentComplete = Math.round(evt.loaded * 100 / evt.total);
61. doms.progress.html(percentComplete.toString() + '%');
62. }
63. },
64. "uploadComplete": function(evt) {
65. alert(evt.target.responseText)
66. }
67. };
68. doms.fileToUpload.on("change", function() {
69. doms.progress.find("span").width("0");
70. funs.fileSelected();
71. });
72. });
73. }
74. });
75.})(Zepto);
調(diào)用方法:
JavaScript Code
1.$(".camera-area").fileUpload({
2. "url": "savetofile.php",
3. "file": "myFile"
4. });
PHP部分:
PHP Code
1.<?php
2.if (isset($_FILES['myFile'])) {
3. // Example:
4. writeLog($_FILES);
5. move_uploaded_file($_FILES['myFile']['tmp_name'], "uploads/" . $_FILES['myFile']['name']);
6. echo 'successful';
7.}
8.function writeLog($log){
9. if(is_array($log) || is_object($log)){
10. $log = json_encode($log);
11. }
12. $log = $log."\r\n";
13.
14. file_put_contents('log.log', $log,FILE_APPEND);
15.}
16.?>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。