HTML5中怎么實(shí)現(xiàn)文件上傳功能,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比臨安網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式臨安網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋臨安地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。
用的技術(shù)主要是:
ajax
FileReader
FormData
HTML結(jié)構(gòu):
JavaScript Code復(fù)制內(nèi)容到剪貼板
<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
JavaScript Code復(fù)制內(nèi)容到剪貼板
(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ù)對(duì)象
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);//上傳前預(yù)覽圖片,也可以通過(guò)其他方法預(yù)覽txt
}
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", funs.uploadProgress, false);//監(jiān)聽上傳進(jìn)度
xhr.addEventListener("load", funs.uploadComplete, false);
xhr.addEventListener("error", opts.uploadFailed, false);
xhr.open("POST", opts.url);
xhr.send(fd);
},
//文件預(yù)覽
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)用方法:
JavaScript Code復(fù)制內(nèi)容到剪貼板
$(".camera-area").fileUpload({
"url": "savetofile.php",
"file": "myFile"
});
PHP部分:
PHP Code復(fù)制內(nèi)容到剪貼板
<?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)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
名稱欄目:HTML5中怎么實(shí)現(xiàn)文件上傳功能
URL鏈接:http://muchs.cn/article16/ihehgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站維護(hù)、手機(jī)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、網(wǎng)站導(dǎo)航、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)