js上传文件:js 上传单个文件(任意大小)



varxml_http,ado_stream;
varSendBlockCount,SendCount;
varlastSendTime,maxFileLimit;
vared=false;
var_filename;
varblockSize=1024*128;
varisUploaded=false;

maxFileLimit=1024*1024*50;//上传文件大小


///处理文件开始上传
functionBeginSend
{
try
{
(document.getElementById(\"UpFileControl\")&&document.getElementById(\"UpFileControl\").value.length>0)
{
document.getElementById(\"speed\").innerHTML=\"0KB/Sec\";//进度条
document.getElementById(\"perent\").innerHTML=\"0%\";
document.getElementById(\"perentBar\").style.width=\"0%\";
document.getElementById(\"bar\").style.display=\"block\";
//document.getElementById(\"control\").style.display=\"block\";
document.getElementById(\"divImportData\").style.display=\"block\";
_filename=document.getElementById(\"UpFileControl\").value;
document.getElementById(\"filename\").innerHTML=\"正在上传文件:\"+_filename.sub(_filename.lastIndexOf(\"\\\\\")+1,_filename.length);

SendFile;
}
(document.getElementById(\"UpFileControl\").value\"\")
{
alert(\"请先选择要上传文件\");
window.parent.AllowFunc; [Page]
document.getElementById(\"btnRe\").disabled=false;
document.getElementById(\"btnUpload\").disabled=false;
document.getElementById(\"UpFileControl\").disabled=false;
Initialize;
false;
}

{
false;
}
}
catch(ex)
{
alert(\"上传文件过程中出错,原因:\"+ex.description);
//window.parent.AllowFunc;
document.getElementById(\"btnRe\").disabled=false;
document.getElementById(\"btnUpload\").disabled=false;
document.getElementById(\"UpFileControl\").disabled=false;
Initialize;
false;
}
}

///开始上传文件
functionSendFile
{
ed=false;
try
{
ado_stream=ActiveXObject(\"ADODB.Stream\");
ado_stream.Type=1;
ado_stream.Open;
}
catch(ex)
{
window.parent.AllowFunc;


document.getElementById(\"btnRe\").disabled=false;
document.getElementById(\"btnUpload\").disabled=false;
document.getElementById(\"UpFileControl\").disabled=false; [Page]
Initialize;
throwError(\"创建数据流对象失败原因:\"+ex.description);
}

try
{
ado_stream.LoadFromFile(document.getElementById(\"UpFileControl\").value);
}
catch(ex)
{
OverSend;
window.parent.AllowFunc;
document.getElementById(\"btnRe\").disabled=false;
document.getElementById(\"btnUpload\").disabled=false;
document.getElementById(\"UpFileControl\").disabled=false;
Initialize;
throwError(\"无法打开文件\"+document.getElementById(\"UpFileControl\").value+\",传输将终止原因\"+ex.description);

;
}

ado_stream.position=0;
(ado_stream.size0)
{
OverSend;
window.parent.AllowFunc;
document.getElementById(\"btnRe\").disabled=false;
document.getElementById(\"btnUpload\").disabled=false;
document.getElementById(\"UpFileControl\").disabled=false;
Initialize;
throwError(\"选择文件为大小为0空文件传输将终止\");
;
}

(ado_stream.size>maxFileLimit)
{
OverSend;
window.parent.AllowFunc; [Page]
document.getElementById(\"btnRe\").disabled=false;
document.getElementById(\"btnUpload\").disabled=false;
document.getElementById(\"UpFileControl\").disabled=false;
Initialize;
throwError(\"您选择上传文件已超过服务器允许上传文件最大值\");
;
}

SendCount=Math.ceil(ado_stream.size/blockSize);
varUpFile=document.getElementById(\"UpFileControl\").value;

SendBlockCount=0;
lastSendTime=Date;


xml_http=CreateXmlHttpObj;

(xml_httpnull)
{
OverSend;
window.parent.AllowFunc;
document.getElementById(\"btnRe\").disabled=false;
document.getElementById(\"btnUpload\").disabled=false;
document.getElementById(\"UpFileControl\").disabled=false;
Initialize;
throwError(\"创建XmlHttp对象失败,请先下载安装MsXMl组件\");

;


}
try
{
SendData;
}
catch(ex)
{
OverSend;
window.parent.AllowFunc;
document.getElementById(\"btnRe\").disabled=false;
document.getElementById(\"btnUpload\").disabled=false; [Page]
document.getElementById(\"UpFileControl\").disabled=false;
Initialize;
throwError(ex.description);
;
}
}

///异步发送文件上传请求到后台
functionSendData
{
(!ed)
{
xml_http.open(\"POST\",\"../upload.aspx?ps=\"+SendBlockCount+\"&userName=\"+param+\"&fileType=\"+extendName,true);//添加个文件类型//不能含有中文//接收上传页面
xml_http.onreadystatechange=ShowPerent;
xml_http.send(ado_stream.Read(blockSize));
}
}

///显示上传进度
functionShowPerent
{
(xml_http.readystate4&&xml_http.status200)
{
(xml_http.responseText!=\"true\")
{
alert(xml_http.responseText);
;
}
SendBlockCount;
varnow=Date;
varperentStat=Math.floor(SendBlockCount/SendCount*100);
perentStat\"%\"

varsendSpeed=blockSize*SendBlockCount/(now.getTime-lastSendTime.getTime)*1000;

(sendSpeed!=\"Infinity\")
{
(sendSpeed<1000)
{
sendSpeed=Math.floor(sendSpeed)+\"\"; [Page]
}

{
sendSpeed=Math.floor(sendSpeed/1024)+\"K\";
}
}

{
sendSpeed=\"0\";
}
document.getElementById(\"speed\").innerHTML=sendSpeed+\"B/Sec\";
document.getElementById(\"perent\").innerHTML=perentStat;
document.getElementById(\"perentBar\").style.width=perentStat;

(SendBlockCount<SendCount)
{
SendData;
}

{
OverSend;


document.getElementById(\"filename\").innerHTML=\"上传文件:\"+_filename.sub(_filename.lastIndexOf(\"\\\\\")+1,_filename.length)+\"成功\";
window.Timeout(\"ImportSudentInfo\",10);
}
}
}

///取消正在上传文件
functionCancelSend
{
(typeof(xml_http)!=’und’&&xml_http!=null)
{
ed=true;
xml_http.abort;
DeleteSingleFile;
OverSend; [Page]
}
}

///结束文件上传
functionOverSend
{
document.getElementById(\"bar\").style.display=\"block\";
document.getElementById(\"divImportData\").style.display=\"block\";
varoTextRange=document.body.createTextRange;
oTextRange.moveToElementText(document.getElementById(\"UpFileControl\"));
oTextRange.execCommand(’Cut’);
oTextRange.execCommand(’Paste’);
(typeof(ado_stream)!=\"und\")
{
ado_stream.Close;
}
ed=false;
}

///删除指定文件
functionDeleteSingleFile
{
varxml_http2=CreateXmlHttpObj;
(xml_http2null)
{
throwError(\"创建XmlHttp对象失败,请先下载安装MsXMl组件\");
;
}
xml_http2.open(\"POST\",\"../upload.aspx?ps=-1\",true);
xml_http2.RequestHeader(\"content-type\",\"application/x-www-form-urlencoded\");
xml_http2.RequestHeader(\"Content-Length\",param.length);
xml_http2.send(null);
}

///创建XMLhttp对象
functionCreateXmlHttpObj
{
try
{ActiveXObject(’MSXML2.XMLHTTP.4.0’);}
catch(e)
{
try
{ActiveXObject(’MSXML2.XMLHTTP.3.0’);}
catch(e)
{
try{ActiveXObject(’MSXML2.XMLHTTP.2.6’);}
catch(e)
{
try{ActiveXObject(’MSXML2.XMLHTTP’);}
catch(e)
{
try{ActiveXObject(’Microsoft.XMLHTTP’);}
catch(e)
{
try{XMLHttpRequest;} [Page]
catch(e)
{
null;
}
}
}
}
}
}
}





upload.aspx接收页面后台代码:

protectedusername;
protectedvoidPage_Load(objectsender,EventArgse)
{
///文件模块编号
partid=Request.QueryString[\"ps\"];
this.username=Request.QueryString[\"userName\"];
fileType=Request.QueryString[\"fileType\"];


(partid\"\"||partidnull||username\"\"||usernamenull)
{
Response.Write(\"系统非法访问!\");
;
}

filename=username+\"考场编排\"+fileType;
strReturn=\"true\";

try
{
///获得文件上传目录
upPath=Path.Combine(AppDo.CurrentDo.BaseDirectory,\"Uploads\");
(!upPath.EndsWith(\"\\\\\")&&!upPath.EndsWith(\"/\"))
{
upPath\"\\\\\"; [Page]
}
(!Directory.Exists(upPath))
{
Directory.CreateDirectory(upPath);
}
strDir=upPath;
///执行文件删除
(partid\"-1\")
{
//filename=Request.Form[\"fns\"].;
(.IO.File.Exists(strDir+filename))
{
.IO.File.Delete(strDir+filename);
}
;
}

///执行文件上传操作
(partid\"0\")
{
(!.IO.Directory.Exists(strDir))
{
.IO.Directory.CreateDirectory(strDir);
}
(.IO.File.Exists(strDir+filename))
{ [Page]
.IO.File.Delete(strDir+filename);
}
}
buffer=Request.BinaryRead(Convert.ToInt32(Request.ContentLength));
.IO.FileStreamFS=.IO.FileStream(strDir+filename,.IO.FileMode.Append);
FS.Write(buffer,0,buffer.Length);
FS.Close;
strReturn=\"true\";



}
catch(Exceptionex)
{
strReturn=\"上传文件出错:\"+ex.Message;

}
finally
{
Response.Clear;
Response.Write(strReturn);
Response.End;
}

}

Tags:  js上传头像 js上传图片 js上传 js上传文件

延伸阅读

最新评论

发表评论