怎样限制上传文件的大小

chate 2010-01-25 10:12:50
下面是我的上传文件代码,可以上传几十兆的大文件。想限制为3M,就直接加了个“If UpLoadAll_a<3145728 Then”的条件语句,却发现上传超过3M的文件时,会出现错误:
--------------------------------
此程序无法显示网页
最可能的原因是:
未连接到 Internet。
该网站遇到了问题。
在地址中可能存在键入错误。

更多信息
此问题可能是由下列各种问题导致的:
Internet 连接已丢失。
该网站暂时不可用。
无法连接到域名服务器(DNS)。
域名服务器(DNS)没有该网站的域的列表。
---------------------------------

有网友说这是IIS对asp的上传文件大小为200k限制所致。可我不加“If UpLoadAll_a<3145728 Then”的条件语句却可以上传几十兆的大文件。
问题出在哪里?请给出具体代码,谢谢!

以下为我的上传源代码,红色部分是我添加的条件语句(已被注释掉):
<%
Response.Buffer = True
Server.ScriptTimeOut=9999999
On Error Resume Next
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<style type="text/css">
<!--
body,input {font-size:12px;}
-->
</style>
<title>文件上传 </title>
</head>
<body id="body">
<%
ExtName = "jpg,gif,png,txt,rar,zip,doc" '允许扩展名
SavePath = "upload" '保存路径
If Right(SavePath,1) <>"/" Then SavePath=SavePath&"/" '在目录后加(/)
CheckAndCreateFolder(SavePath)

UpLoadAll_a = Request.TotalBytes '取得客户端全部内容
If(UpLoadAll_a>0) Then
'--------条件语句开始--------
'If UpLoadAll_a<145728 Then
' Response.write "文件小于3M:<br />"

'----------------------------
Set UploadStream_c = Server.CreateObject("ADODB.Stream")
UploadStream_c.Type = 1
UploadStream_c.Open
UploadStream_c.Write Request.BinaryRead(UpLoadAll_a)
UploadStream_c.Position = 0

FormDataAll_d = UploadStream_c.Read
CrLf_e = chrB(13)&chrB(10)
FormStart_f = InStrB(FormDataAll_d,CrLf_e)
FormEnd_g = InStrB(FormStart_f+1,FormDataAll_d,CrLf_e)

Set FormStream_h = Server.Createobject("ADODB.Stream")
FormStream_h.Type = 1
FormStream_h.Open
UploadStream_c.Position = FormStart_f + 1
UploadStream_c.CopyTo FormStream_h,FormEnd_g-FormStart_f-3
FormStream_h.Position = 0
FormStream_h.Type = 2
FormStream_h.CharSet = "GB2312"
FormStreamText_i = FormStream_h.Readtext
FormStream_h.Close

FileName_j = Mid(FormStreamText_i,InstrRev(FormStreamText_i,"""")+1,FormEnd_g)

If(CheckFileExt(FileName_j,ExtName)) Then
SaveFile = Server.MapPath(SavePath & FileName_j)

If Err Then
Response.Write "文件上传: <span style=""color:red;"">文件上传出错! </span>  <a href=""" & Request.ServerVariables("URL") &""">重新上传文件 </a> <br />"
Err.Clear
Else
SaveFile = CheckFileExists(SaveFile)

k=Instrb(FormDataAll_d,CrLf_e&CrLf_e)+4
l=Instrb(k+1,FormDataAll_d,leftB(FormDataAll_d,FormStart_f-1))-k-2
FormStream_h.Type=1
FormStream_h.Open
UploadStream_c.Position=k-1
UploadStream_c.CopyTo FormStream_h,l
FormStream_h.SaveToFile SaveFile,2

SaveFileName = Mid(SaveFile,InstrRev(SaveFile,"\")+1)
Response.write "文件上传: <span style=""color:red;"">" & SaveFileName & " </span>文件上传成功!  <a href=""" & Request.ServerVariables("URL") &""">继续上传文件 </a> <br />"
End If
Else
Response.write "文件上传: <span style=""color:red;"">文件格式不正确! </span>  <a href=""" & Request.ServerVariables("URL") &""">重新上传文件 </a> <br />"
End If
'-------条件语句结束--------
'Else
' Response.write "文件超过3M:<br />"
'End If

'---------------------------
Else
%>
<script language="Javascript">
<!--
function ValidInput()
{

if(document.upform.upfile.value=="")
{
alert("请选择上传文件!")
document.upform.upfile.focus()
return false
}
return true
}
// -->
</script>
<form action=' <%= Request.ServerVariables("URL") %>' method='post' name="upform" onsubmit="return ValidInput()" enctype="multipart/form-data">
文件上传:
<input type='file' name='upfile' size="40"> <input type='submit' value="上传">
</form>
<%
End if
Set FormStream_h = Nothing
UploadStream.Close
Set UploadStream = Nothing
%>
</body>
</html>
<%
'判断文件类型是否合格
Function CheckFileExt(FileName,ExtName) '文件名,允许上传文件类型
FileType = ExtName
FileType = Split(FileType,",")
For i = 0 To Ubound(FileType)
If LCase(Right(FileName,3)) = LCase(FileType(i)) then
CheckFileExt = True
Exit Function
Else
CheckFileExt = False
End if
Next
End Function

'检查上传文件夹是否存在,不存在则创建文件夹
Function CheckAndCreateFolder(FolderName)
fldr = Server.Mappath(FolderName)
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fldr) Then
fso.CreateFolder(fldr)
End If
Set fso = Nothing
End Function

'检查文件是否存在,重命名存在文件
Function CheckFileExists(FileName)
Set fso=Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(SaveFile) Then
i=1
msg=True
Do While msg
CheckFileExists = Replace(SaveFile,Right(SaveFile,4),"_" & i & Right(SaveFile,4))
If not fso.FileExists(CheckFileExists) Then
msg=False
End If
i=i+1
Loop
Else
CheckFileExists = FileName
End If
Set fso=Nothing
End Function
%>
...全文
2275 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
chate 2010-02-25
  • 打赏
  • 举报
回复
DDDDDDDDDD
chate 2010-02-19
  • 打赏
  • 举报
回复
dddddddddd
chate 2010-02-10
  • 打赏
  • 举报
回复
引用 5 楼 mowanglijiang 的回复:
用flash带的上传功能,那里面可以取到文件大小的,超过的不允许上传就行了,现在浏览器都支持flash了。
这种代码,你搞定一次后,以后一直可以用了,好好研究下吧
在这里下载过ASP版的SWFUpload,不能上传带中文的文件,网上似乎没有解决办法,所以才问这个问题的!
上海程序员3 2010-02-10
  • 打赏
  • 举报
回复
用flash带的上传功能,那里面可以取到文件大小的,超过的不允许上传就行了,现在浏览器都支持flash了。
这种代码,你搞定一次后,以后一直可以用了,好好研究下吧
chate 2010-01-28
  • 打赏
  • 举报
回复
修改IIS最大上传限制也解决不了根本问题,即使设置为10M,只要上传超过10M的文件,页面也会出错:
Request 对象 错误 'ASP 0104 : 80004005'
不允许操作:UploadStream_c.Write Request.BinaryRead(UpLoadAll_a)
在这种情况下,即使在程序里限定最大上传文件为100K,用户一旦上传10M以上的文件也会出错的!

请问各路高手有没有解决办法?
chate 2010-01-27
  • 打赏
  • 举报
回复
顶!
快要顶不动了~~~~~~~~
chate 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 amu1433 的回复:]
if fileExt="gif" then
if file.filesize>(3000*1024) then
response.write " <span style=""font-family: 宋体; font-size: 9pt"">最大只能上传 3M 的图片文件! [ <a href=# onclick=history.go(-1)>重新上传 </a> ] </span>"
response.end
end if
end if


这样写 你看行不 
[/Quote]
我测试通不过,你测试过吗?
另外,这一句
FileName_j = Mid(FormStreamText_i,InstrRev(FormStreamText_i,"""")+1,FormEnd_g)
应改为
FileName_j = Mid(FormStreamText_i,InstrRev(FormStreamText_i,"\")+1,FormEnd_g)
是昨天一个已结贴误导所致:
http://topic.csdn.net/u/20100102/00/2b086f89-c3dc-4965-ab4b-eaa2eefa0674.html?47255
谁帮我顶一下,我发言三次不能自己再回复了。可这个结果不对!以免误导他人!!!
amu1433 2010-01-26
  • 打赏
  • 举报
回复
if fileExt="gif" then
if file.filesize>(3000*1024) then
response.write "<span style=""font-family: 宋体; font-size: 9pt"">最大只能上传 3M 的图片文件! [ <a href=# onclick=history.go(-1)>重新上传</a> ]</span>"
response.end
end if
end if



这样写 你看行不

28,392

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧