js實(shí)現(xiàn)分割上傳大文件
本文實(shí)例介紹了js上傳文件操作,分享給大家供大家參考,具體內(nèi)容如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>分割大文件上傳</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
#test{
width: 200px;
height: 100px;
border: 1px solid green;
display: none;
}
#img{
width: 50px;
height: 50px;
display: none;
}
#upimg{
text-align: center;
font: 8px/10px '微軟雅黑','黑體',sans-serif;
width: 300px;
height: 10px;
border: 1px solid green;
}
#load{
width: 0%;
height: 100%;
background: green;
text-align: center;
}
</style>
</head>
<body>
<form enctype="multipart/form-data" action="file.php" method="post">
<!--
<input type="file" name="pic" />
<div id="img"></div>
<input type="button" value="uploadimg" onclick="upimg();" /><br />
-->
<div id="upimg">
<div id="load"></div>
</div>
<input type="file" name="mof" multiple="multiple"/>
<input type="button" value="uploadfile" onclick="upfile();" />
<input type="submit" value="submit" />
</form>
<div id="test">
測(cè)試是否DIV消失
</div>
<script type="text/javascript">
var dom=document.getElementsByTagName('form')[0];
dom.onsubmit=function(){
//return false;
}
var xhr=new XMLHttpRequest();
var fd;
var des=document.getElementById('load');
var num=document.getElementById('upimg');
var file;
const LENGTH=10*1024*1024;
var start;
var end;
var blob;
var pecent;
var filename;
//var pending;
//var clock;
function upfile(){
start=0;
end=LENGTH+start;
//pending=false;
file=document.getElementsByName('mof')[0].files[0];
//filename = file.name;
if(!file){
alert('請(qǐng)選擇文件');
return;
}
//clock=setInterval('up()',1000);
up();
}
function up(){
/*
if(pending){
return;
}
*/
if(start<file.size){
xhr.open('POST','file.php',true);
//xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.onreadystatechange=function(){
if(this.readyState==4){
if(this.status>=200&&this.status<300){
if(this.responseText.indexOf('failed') >= 0){
//alert(this.responseText);
alert('文件發(fā)送失敗,請(qǐng)重新發(fā)送');
des.style.width='0%';
//num.innerHTML='';
//clearInterval(clock);
}else{
//alert(this.responseText)
// pending=false;
start=end;
end=start+LENGTH;
setTimeout('up()',1000);
}
}
}
}
xhr.upload.onprogress=function(ev){
if(ev.lengthComputable){
pecent=100*(ev.loaded+start)/file.size;
if(pecent>100){
pecent=100;
}
//num.innerHTML=parseInt(pecent)+'%';
des.style.width=pecent+'%';
des.innerHTML = parseInt(pecent)+'%'
}
}
//分割文件核心部分slice
blob=file.slice(start,end);
fd=new FormData();
fd.append('mof',blob);
fd.append('test',file.name);
//console.log(fd);
//pending=true;
xhr.send(fd);
}else{
//clearInterval(clock);
}
}
function change(){
des.style.width='0%';
}
</script>
</body>
</html>
file.php:
<?php
/****
waited
****/
//print_r($_FILES);exit;
$file = $_FILES['mof'];
$type = trim(strrchr($_POST['test'], '.'),'.');
// print_r($_POST['test']);exit;
if($file['error']==0){
if(!file_exists('./upload/upload.'.$type)){
if(!move_uploaded_file($file['tmp_name'],'./upload/upload.'.$type)){
echo 'failed';
}
}else{
$content=file_get_contents($file['tmp_name']);
if (!file_put_contents('./upload/upload.'.$type, $content,FILE_APPEND)) {
echo 'failed';
}
}
}else{
echo 'failed';
}
?>
1 運(yùn)行:

2 選擇2G文件測(cè)試:

3 完成并正常播放:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
js對(duì)象數(shù)組和對(duì)象的使用實(shí)例詳解
在本篇文章里小編給大家整理了關(guān)于js對(duì)象數(shù)組和對(duì)象的使用實(shí)例相關(guān)知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。2019-08-08
如何通過(guò)javaScript去除字符串兩端的空白字符
這篇文章主要介紹了如何通過(guò)javaScripte去除字符串兩端的空白字符,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
javascript重寫(xiě)alert方法的實(shí)例代碼
最近因?yàn)樵趂lash項(xiàng)目中遇到as調(diào)用js函數(shù)馬上彈出alert,在火狐瀏覽器下會(huì)卡死瀏覽器。介于這樣我們重寫(xiě)alert,讓alert彈出一個(gè)自定義的div彈層,來(lái)實(shí)現(xiàn)提示效果!2013-03-03
Bootstrap入門(mén)書(shū)籍之(三)柵格系統(tǒng)
這篇文章主要介紹了Bootstrap入門(mén)書(shū)籍之(三)柵格系統(tǒng)的相關(guān)資料,需要的朋友可以參考下2016-02-02
JavaScript實(shí)現(xiàn)動(dòng)態(tài)網(wǎng)頁(yè)飄落的雪花
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)動(dòng)態(tài)網(wǎng)頁(yè)飄落的雪花,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
谷歌瀏覽器不支持showModalDialog模態(tài)對(duì)話(huà)框的解決方法
谷歌瀏覽器不支持showModalDialog模態(tài)對(duì)話(huà)框和無(wú)法返回returnValue,這個(gè)問(wèn)題,想必很多朋友都有遇到過(guò)吧,解決方法很簡(jiǎn)單,下面的思路,大家可以看看2014-09-09

