javascript html5移動端輕松實現(xiàn)文件上傳
PC端上傳文件多半用插件,引入flash都沒關系,但是移動端要是還用各種冗余的插件估計得被噴死,項目里面需要做圖片上傳的功能,既然H5已經(jīng)有相關的接口且兼容性良好,當然優(yōu)先考慮用H5來實現(xiàn)。
用的技術主要是:
- ajax
- FileReader
- FormData
HTML結構:
<div class="camera-area"> <form enctype="multipart/form-data" method="post"> <input type="file" name="fileToUpload" class="fileToUpload" accept="image/*" capture="camera"/> <div class="upload-progress"><span></span></div> </form> <div class="thumb"></div> </div>
已經(jīng)封裝好的upload.js,依賴zepto
(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 = { //選擇文件,獲取文件大小,也可以在這里獲取文件格式,限制用戶上傳非要求格式的文件 "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 > 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(); }, //異步上傳文件 uploadFile: function() { var fd = new FormData();//創(chuàng)建表單數(shù)據(jù)對象 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);//將文件添加到表單數(shù)據(jù)中 funs.previewImage(file);//上傳前預覽圖片,也可以通過其他方法預覽txt } var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", funs.uploadProgress, false);//監(jiān)聽上傳進度 xhr.addEventListener("load", funs.uploadComplete, false); xhr.addEventListener("error", opts.uploadFailed, false); xhr.open("POST", opts.url); xhr.send(fd); }, //文件預覽 previewImage: function(file) { var gallery = doms.thumb; var img = document.createElement("img"); img.file = file; doms.thumb.html(img); // 使用FileReader方法顯示圖片內(nèi)容 var reader = new FileReader(); reader.onload = (function(aImg) { return function(e) { aImg.src = e.target.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.target.responseText) } }; doms.fileToUpload.on("change", function() { doms.progress.find("span").width("0"); funs.fileSelected(); }); }); } }); })(Zepto);
調(diào)用方法:
$(".camera-area").fileUpload({ "url": "savetofile.php", "file": "myFile" });
PHP部分:
<?php 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); } ?>
更多精彩內(nèi)容請參考專題《ajax上傳技術匯總》,《javascript文件上傳操作匯總》和《jQuery上傳操作匯總》進行學習。
希望本文所述對大家學習有所幫助。
相關文章
解決window.opener=null;window.close(),只支持IE6不支持IE7,IE8的問題
本篇文章主要是對window.opener=null;window.close(),只支持IE6不支持IE7,IE8的解決方法進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01如何利用 JS 腳本實現(xiàn)網(wǎng)頁全自動秒殺搶購功能
這篇文章主要介紹了如何利用 JS 腳本實現(xiàn)網(wǎng)頁全自動秒殺搶購功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10js實現(xiàn)的頁面加載完畢之前l(fā)oading提示效果完整示例【附demo源碼下載】
這篇文章主要介紹了js實現(xiàn)的頁面加載完畢之前l(fā)oading提示效果,結合完整實例形式分析了js頁面加載時顯示loading效果的實現(xiàn)技巧,需要的朋友可以參考下2016-08-08微信小程序第三方框架對比 之 wepy / mpvue / taro
這篇文章主要介紹了小程序第三方框架對比 ( wepy / mpvue / taro ) 分析,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2019-04-04