<iframe frameborder=no border=0 marginwidth=0 marginheight=0 scrolling=no allowtransparency=yes src=co_upload.asp?cmd="&cmd&"&EditName=co_logo&filelx=|.jpg|.png|.gif|&divname=logo2 height=30 width=390></iframe>
co_upload.asp
---------------------------------------------------------------------------------------
<%
filelx=request("filelx") '文件上传类型
EditName=request("EditName") '回传到上页面编辑框的Name
divname=request("divname")
cmd=request("cmd")
z_id=request("z_id")
data_type=request("data_type")
page_id=request("page_id")
mb_order=request("mb_order")
%>
<html>
<head>
<title>图片上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
body,td{font-size:12px;}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.btn{
BORDER-RIGHT: #2C59AA 1px solid; PADDING-RIGHT: 2px; BORDER-TOP:
#2C59AA 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 12px; FILTER:
progid:DXImageTransform.Microsoft.Gradient(GradientType=0,
StartColorStr=#ffffff, EndColorStr=#C3DAF5); BORDER-LEFT: #2C59AA
1px solid; CURSOR: hand; COLOR: black; PADDING-TOP: 2px;
BORDER-BOTTOM: #2C59AA 1px solid
}
</style>
<%
dim SPid,PID,barref
Set UploadProgress = Server.CreateObject("Persits.UploadProgress")
SPid = UploadProgress.CreateProgressID()
PID = "PID=" & SPid
barref = "/framebar.asp?to=10&"&PID
%>
<script language='javascript'>
function ShowProgress() //显示上传进度
{
strAppVersion = navigator.appVersion;
if (strAppVersion.indexOf('MSIE') != -1 && strAppVersion.substr(strAppVersion.indexOf('MSIE')+5,1) > 4)
{
winstyle = "dialogWidth=375px; dialogHeight:175px; center:yes;status:no;help:no;";
window.showModelessDialog('<% = barref %>&b=IE',window,winstyle);
}
else
{
window.open('<% = barref %>&b=NN','','width=370,height=165', true);
}
return true;
}
/*----------------------------------------
文件上传前台控制检测程序 v0.6
js检测上传文件类型
js检测图片文件格式是否正确
js检测图片文件大小
js检测图片文件宽度
js检测图片文件高度
-----------------------------------------*/
var ImgObj=new Image(); //建立一个图像对象
var AllImgExt="|.jpg|.png|.jpeg|.gif|"//全部图片格式类型
var FileObj,ImgFileSize,ImgWidth,ImgHeight,FileExt,ErrMsg,FileMsg,HasCheked,IsImg//全局变量图片相关属性
//以下为限制变量
var AllowExt="<%=filelx%>" //允许上传的文件类型 ŀ为无限制 每个扩展名后边要加一个"|" 小写字母表示//var AllowExt=0
var AllowImgFileSize=0; //允许上传图片文件的大小 0为无限制 单位:KB
var AllowImgWidth=0; //允许上传的图片的宽度 ŀ为无限制 单位:px(像素)
var AllowImgHeight=0; //允许上传的图片的高度 ŀ为无限制 单位:px(像素)
function CheckProperty(obj) //检测图像属性
{
FileObj=obj;
ImgObj.onerror=function(){ErrMsg='/n图片格式不正确或者图片已损坏!';}
if(ErrMsg!="") //检测是否为正确的图像文件 返回出错信息并重置
{
ShowMsg(ErrMsg);
return false;
}
if(ImgObj.readyState!="complete") //如果图像是未加载完成进行循环检测
{
setTimeout("CheckProperty(FileObj)",500);
return false;
}
ImgFileSize=Math.round(ImgObj.fileSize/1024*100)/100;//取得图片文件的大小
ImgWidth=ImgObj.width //取得图片的宽度
ImgHeight=ImgObj.height; //取得图片的高度
if(AllowImgWidth!=0&&AllowImgWidth<ImgWidth)
ErrMsg=ErrMsg+"/n图片宽度超过限制。请上传宽度小于"+AllowImgWidth+"px的文件,当前图片宽度为"+ImgWidth+"px";
if(AllowImgHeight!=0&&AllowImgHeight<ImgHeight)
ErrMsg=ErrMsg+"/n图片高度超过限制。请上传高度小于"+AllowImgHeight+"px的文件,当前图片高度为"+ImgHeight+"px";
if(AllowImgFileSize!=0&&AllowImgFileSize<ImgFileSize)
ErrMsg=ErrMsg+"/n图片文件大小超过限制。请上传小于"+AllowImgFileSize+"KB的文件,当前文件大小为"+ImgFileSize+"KB";
if(ErrMsg!="")
{
ShowMsg(ErrMsg);
return false;
}
}
function ShowMsg(msg)
{
FileObj.outerHTML=FileObj.outerHTML;
alert(msg);
return false;
}
function CheckExt(obj) //判断文件类型
{
ErrMsg="";
FileObj=obj;
if(obj.value=="")return false;
FileExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();
if(AllowExt!=0&&AllowExt.indexOf("|"+FileExt+"|")==-1) //判断文件类型是否允许上传
{
ErrMsg="/n该文件类型不允许上传。请上传 "+AllowExt+" 类型的文件,当前文件类型为"+FileExt;
ShowMsg(ErrMsg);
return false;
}
//if(AllImgExt.indexOf("|"+FileExt+"|")!=-1) //如果图片文件,对大小长宽正确进行判断
//{
//ImgObj.src=obj.value;
//CheckProperty(obj);
//return false;
//}
}
</script>
<script language="javascript">
function CheckData() {
if (!Check(form1.file1,"请选择文件!")) return false;
form1.Submit.disabled = true;
ShowProgress();
return true;
}
function Check(obj,msg) {
if (obj.value == "") {
alert(msg);
obj.focus();
return false;
} else {
return true;
}
}
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000" oncontextmenu=window.event.returnValue=false scroll=no>
<table width="100%" height="30" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#CCCCFF">
<form name="form1" method="post" action="co_upfile.asp?<% = PID %>&cmd=<%=cmd%>&z_id=<%=z_id%>&data_type=<%=data_type%>&page_id=<%=page_id%>&mb_order=<%=mb_order%>" onSubmit="return CheckData();" enctype="multipart/form-data" >
<tr align="center" valign="middle">
<td width="17%" align="center" bgcolor="#99C0DD" id="upid"> 选择文件:
<input type="hidden" name="filelx" value="<%=filelx%>">
<input type="hidden" name="EditName" value="<%=EditName%>">
<input type="hidden" name="divname" value="<%=divname%>"></td>
<td width="83%" align="left" bgcolor="#EDEEEF" id="upid"><input type="file" name="file1" style="width:230px;height:20px;" onChange="javascript:CheckExt(this)" class=btn >
<input type="submit" name="Submit" value="开始上传" class=btn ></td>
</tr>
</form>
</table>
</body>
</html>
---------------------------------------------------------------------------------------
framebar.asp
---------------------------------------------------------------------------------------
<%@EnableSessionState=False%>
<% Response.Expires = -1 %>
<title>正在上传................................................</title>
<style type='text/css'>td {font-family:Tahoma; font-size: 8pt }</style>
<BODY BGCOLOR="menu" scroll="NO" frameborder="NO" status="no" style="border:0px;">
<script language="JavaScript">
<!--
function Stopupload()
{
if (typeof(window.opener)!="undefined"){
try{
window.opener.document.execCommand("stop");
}
catch(e){}
}
else if(typeof(window.dialogArguments)!="undefined"){
try{
window.dialogArguments.document.execCommand("stop");
}
catch(e){}
}
window.close();
}
//-->
</script>
<IFRAME src="bar.asp?PID=<%= Request("PID") & "&to=" & Request("to") %>" title="Uploading" noresize scrolling=no frameborder=0 framespacing=10 width=369 height=115></IFRAME>
<TABLE BORDER="0" WIDTH="100%" cellpadding="2" cellspacing="0">
<TR><TD ALIGN="center"><button onClick="Stopupload()" style="font-size:12px;">取消上传</button>
</TD></TR>
</TABLE>
</BODY>
</HTML>
---------------------------------------------------------------------------------------
bar.asp
---------------------------------------------------------------------------------------
<%
Response.Expires = -1
PID = Request("PID")
TimeO = Request("to")
Set UploadProgress = Server.CreateObject("Persits.UploadProgress")
format = "<br><CENTER><b>正在上传,请耐心等待...</b></CENTER><br>%T%t%B3%T 速度:(%S/秒) 估计剩余时间:%R %r%U/%V(%P)%l%t"
bar_content = UploadProgress.FormatProgress(PID, TimeO, "#00007F", format)
If "" = bar_content Then
%>
<HTML>
<HEAD>
<TITLE>Upload Finished</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function CloseMe()
{
window.parent.close();
return true;
}
</SCRIPT>
</HEAD>
<BODY OnLoad="CloseMe()" BGCOLOR="menu">
</BODY>
</HTML>
<%
Else ' Not finished yet
%>
<HTML>
<HEAD>
<meta HTTP-EQUIV="Refresh" CONTENT="1;URL=<%
Response.Write Request.ServerVariables("URL")
Response.Write "?to=" & TimeO & "&PID=" & PID %>">
<TITLE>Uploading Files...</TITLE>
<style type="text/css">
body,td {font-family:Tahoma; font-size: 8pt }
td.spread {font-size: 6pt; line-height:6pt }
td.brick {font-size:6pt; height:12px}
</style>
</HEAD>
<BODY BGCOLOR="menu" topmargin=0>
<% = bar_content %>
</BODY>
</HTML>
<% End If %>
---------------------------------------------------------------------------------------
co_upfile.asp
---------------------------------------------------------------------------------------
<!--#Include file="co_is_login.asp" -->
<!--#include virtual="/inc/conn.asp"-->
<%
'on error resume next
cmd = Request("cmd")
select case cmd
case 1
sql="select * from mc_co where id="&session("co_user_id")
set rs=conn.execute(sql)
if not rs.eof then
UploadFilePath="/public/images/yllogo/"
Set Upload=Server.CreateObject("Persits.Upload")
'Upload.SetMaxSize 1024*1024, True
Upload.OverwriteFiles = false
if Request.QueryString("PID") = "" then
Upload.ProgressID="010D60EB00C5AA4B"
else
Upload.ProgressID=Request.QueryString("PID")
end if
Count=Upload.SaveVirtual(UploadFilePath) '虚拟路径
EditName=Upload.form("EditName")
divname=Upload.form("divname")
For Each File in Upload.Files
NewName = session("co_user_id")&File.ext
if Upload.FileExists(server.mappath(UploadFilePath & NewName)) then
Upload.DeleteFile server.mappath(UploadFilePath & NewName)
end if
File.MoveVirtual UploadFilePath & NewName '移动
sql="update mc_co set co_logo='"&UploadFilePath&NewName&"' where id="&session("co_user_id")
conn.execute(sql)
Response.Write "<script src='save_yl_xml.asp?co_id="&session("co_user_id")&"'></script>"
response.write "<script>parent.Tit_value('"&EditName&"','"&divname&"','"&UploadFilePath & NewName&"');alert('上传成功!');</script>"
next
end if
set rs=nothing
case 2
sql="select * from mc_co where id="&session("co_user_id")
set rs=conn.execute(sql)
if not rs.eof then
UploadFilePath="/public/images/ylgg/"
Set Upload=Server.CreateObject("Persits.Upload")
'Upload.SetMaxSize 1024*1024, True
Upload.OverwriteFiles = false
if Request.QueryString("PID") = "" then
Upload.ProgressID="010D60EB00C5AA4B"
else
Upload.ProgressID=Request.QueryString("PID")
end if
Count=Upload.SaveVirtual(UploadFilePath) '虚拟路径
EditName=Upload.form("EditName")
divname=Upload.form("divname")
For Each File in Upload.Files
NewName = session("co_user_id")&File.ext
if Upload.FileExists(server.mappath(UploadFilePath & NewName)) then
Upload.DeleteFile server.mappath(UploadFilePath & NewName)
end if
File.MoveVirtual UploadFilePath & NewName '移动
sql="update mc_co set co_gg='"&UploadFilePath&NewName&"' where id="&session("co_user_id")
conn.execute(sql)
Response.Write "<script src='save_yl_xml.asp?co_id="&session("co_user_id")&"'></script>"
response.write "<script>parent.Tit_value('"&EditName&"','"&divname&"','"&UploadFilePath & NewName&"');alert('上传成功!');</script>"
next
end if
case 3
z_id=request("z_id")
sql="select * from zine_zine where z_id="&z_id&" and c_id="&session("co_user_id")
set rs=conn.execute(sql)
if not rs.eof then
UploadFilePath="/zine/"&z_id&"/images/"
Set Upload=Server.CreateObject("Persits.Upload")
Upload.OverwriteFiles = false
if Request.QueryString("PID") = "" then
Upload.ProgressID="010D60EB00C5AA4B"
else
Upload.ProgressID=Request.QueryString("PID")
end if
Count=Upload.SaveVirtual(UploadFilePath) '虚拟路径
EditName=Upload.form("EditName")
divname=Upload.form("divname")
For Each File in Upload.Files
NewName = "bj.jpg"
if Upload.FileExists(server.mappath(UploadFilePath & NewName)) then
Upload.DeleteFile server.mappath(UploadFilePath & NewName)
end if
File.MoveVirtual UploadFilePath & NewName '移动
sql="update zine_zine set bjurl='"&NewName&"' where z_id="&z_id
conn.execute(sql)
next
Response.Write "<script src='save_zine_xml.asp?z_id="&z_id&"'></script>"
response.write "<script>parent.Tit_value('"&EditName&"','"&divname&"','"&NewName&"');alert('上传成功!');</script>"
end if
set rs=nothing
case 4
data_type=request("data_type")
z_id=request("z_id")
page_id=request("page_id")
mb_order=request("mb_order")
Set Upload=Server.CreateObject("Persits.Upload")
Upload.OverwriteFiles = false
UploadFilePath="/zine/"&z_id&"/images/"
Count=Upload.SaveVirtual(UploadFilePath) '虚拟路径
For Each File in Upload.Files
ranNum=int(90000*rnd)+10000
NewName = year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&File.ext
File.MoveVirtual UploadFilePath & NewName '移动
sql1="select * from zine_img where z_id="&z_id&" and page_id="&page_id&" and mb_order="&mb_order
set rs1=conn.execute(sql1)
if not rs1.eof then
if Upload.FileExists(server.mappath(UploadFilePath & rs1("img"))) then
Upload.DeleteFile server.mappath(UploadFilePath & rs1("img"))
end if
'Return_value=DelFile(uppath&rs1("img"))
sql="update zine_img set img='"&NewName&"' where z_id="&z_id&" and page_id="&page_id&" and mb_order="&mb_order
else
sql="insert into zine_img (z_id,page_id,mb_order,img) values ("&z_id&","&page_id&","&mb_order&",'"&NewName&"')"
end if
conn.execute(sql)
Response.Write "<script src='save_page_xml.asp?page_id="&page_id&"'></script>"
response.write "<script>parent.update('file_"&page_id&"_"&mb_order&"','return_co_upfile.asp','GET','cmd=4&data_type="&data_type&"&z_id="&z_id&"&page_id="&page_id&"&mb_order="&mb_order&"');</script>"
response.write "<script>parent.update('"&page_id&"_"&mb_order&"','return_zine_data_type.asp','GET','cmd="&data_type&"&z_id="&z_id&"&page_id="&page_id&"&mb_order="&mb_order&"');</script>"
response.write "<script>alert('上传成功!');</script>"
next
case 5
data_type=request("data_type")
z_id=request("z_id")
page_id=request("page_id")
mb_order=request("mb_order")
Set Upload=Server.CreateObject("Persits.Upload")
Upload.OverwriteFiles = false
UploadFilePath="/zine/"&z_id&"/flv/"
Count=Upload.SaveVirtual(UploadFilePath) '虚拟路径
For Each File in Upload.Files
ranNum=int(90000*rnd)+10000
NewName = year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&File.ext
File.MoveVirtual UploadFilePath & NewName '移动
flvname=UploadFilePath & NewName
sql1="select * from zine_flv where z_id="&z_id&" and page_id="&page_id&" and mb_order="&mb_order
set rs1=conn.execute(sql1)
if not rs1.eof then
flv=rs1("flv")
if instr(flv,"//")=0 and instr(flv,"/zine/"&z_id&"/flv/")>0 and flv<>"/public/flv/today.flv" then
if Upload.FileExists(server.mappath(flv)) then
Upload.DeleteFile server.mappath(flv)
end if
end if
sql="update zine_flv set flv='"&flvname&"' where z_id="&z_id&" and page_id="&page_id&" and mb_order="&mb_order
else
sql="insert into zine_flv (z_id,page_id,mb_order,flv) values ("&z_id&","&page_id&","&mb_order&",'"&flvname&"')"
end if
conn.execute(sql)
Response.Write "<script src='save_page_xml.asp?page_id="&page_id&"'></script>"
response.write "<script>parent.update('file_"&page_id&"_"&mb_order&"','return_co_upfile.asp','GET','cmd=5&data_type="&data_type&"&z_id="&z_id&"&page_id="&page_id&"&mb_order="&mb_order&"');</script>"
response.write "<script>parent.update('"&page_id&"_"&mb_order&"','return_zine_data_type.asp','GET','cmd="&data_type&"&z_id="&z_id&"&page_id="&page_id&"&mb_order="&mb_order&"');</script>"
response.write "<script>alert('上传成功!');</script>"
next
end select
%>
---------------------------------------------------------------------------------------
本文介绍了一个基于ASP的图片上传组件实现细节,包括文件类型的验证、尺寸限制、上传进度显示等功能,并展示了完整的代码实现。
1万+

被折叠的 条评论
为什么被折叠?



