艺术迷网
艺术迷 >> 网页设计 >> 其它教程 >> 如何使用AspUpload组件上传文件
2006-9-14   作者:佚名   来源:艺术迷收集(fansart.com)   点击:2137
页面功能: 【评论】有错就点】 【字体: 】 【打印】 【关闭【收藏本页】收藏到365Key
 
使用AspUpload组件上传文件,使用范例代码如下: AspUpload组件上传测试
<form method="POST" enctype="multipart/form-data" action="?act=upload">
<input type="file" size="40" name="file1"><br>
<!--<input type="file" size="40" name="file2"><br>
<input type="file" size="40" name="file3"><br>-->
<br>其他表单项<input type="text" name="uploadText" value=""><br>
<br><input type="submit" value="上传文件">
</form>
<%
if request("act") = "upload" then

AllowExt = "jpg,png,gif,zip,rar,sql,txt,bak"
On Error Resume Next

' 新建AspUpload对象
Set Upload = Server.CreateObject("Persits.Upload")

' 限制文件大小
Upload.SetMaxSize 4194304, True

' 上传路径--当前目录下的test目录
uploadDir = Server.MapPath("test")

' 尝试创建路径文件夹,true表示忽略目录已存在错误
Upload.CreateDirectory uploadDir, true

' 先上传文件至服务器内存
Count = Upload.Save()

' 检测上传错误
If Err.Number = 8 Then
Response.Write chinese2unicode("错误: 文件过大!")
Response.end
Else
If Err <> 0 Then
response.write chinese2unicode("发生错误:")
response.write chinese2unicode(Err.Description)
response.end
End If
End If

'Response.Write chinese2unicode("共 " & Count & " 个文件") & "<br><br>"

' 指定一个上传的表单文件
Set File = Upload.Files("file1")
If Not File Is Nothing Then
' 获取原本文件名
Filename = File.Filename
' 获取文件扩展名
Fileext = File.Ext

' 检测文件格式是否合格
ChkStr = ","&Lcase(AllowExt)&","
If Instr(ChkStr,","&right(Fileext,3)&",") <= 0 Then
Response.Write chinese2unicode("错误: 文件类型不正确!")
response.write "<br>"
response.write chinese2unicode("只允许:"&AllowExt)
' 删除内存中的临时文件,以释放内存或硬盘空间(还可用Copy、Move两个指令)
File.Delete
' 检测是否存在文件
elseif Upload.FileExists(uploadDir & "\" & Filename) Then
File.SaveAs uploadDir & "\" & Filename
Response.Write chinese2unicode("已覆盖存在相同文件名的文件: ") & File.Path
' 保存文件
else
File.SaveAs uploadDir & "\" & Filename
Response.Write chinese2unicode("文件已保存到: ") & File.Path
end If
Else
Response.Write chinese2unicode("错误: 您并没有选择文件!")
End If

Response.Write "<br><br>"

'' 批量上传文件,去掉注释即可用。
For Each File in Upload.Files
'File.SaveAs uploadDir & "\" & File.FileName
'Response.Write chinese2unicode("文件已保存到: ") & File.Path & "<br>"
Next


'Response.Write "<br><br>Files:<br>"
For Each File in Upload.Files
'Response.Write File.Name & "= " & File.Path & " (" & File.Size &" bytes)<br>"
Next

'' 列出其他表单内容(必须执行Upload.Save()后才有效)
For Each Item in Upload.Form
Response.Write Item.Name & " = " & Item.Value & "<br>"
Next

'列出指定的表单内容
Response.Write "<br>"&chinese2unicode("列出指定内容uploadText:"&Upload.Form("uploadText").value)



end if



' gb2312转unicode,解决中文乱码问题
function chinese2unicode(Str)
dim i
dim Str_one
dim Str_unicode
for i=1 to len(Str)
Str_one=Mid(Str,i,1)
Str_unicode=Str_unicode&chr(38)
Str_unicode=Str_unicode&chr(35)
Str_unicode=Str_unicode&chr(120)
Str_unicode=Str_unicode& Hex(ascw(Str_one))
Str_unicode=Str_unicode&chr(59)
next
Response.Write Str_unicode
end function
%>



声明:本站刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。如有疑问请与站长联系。
网友评论
目前没有评论!
发表评论
您的姓名: 匿名发送
电子邮件:
评论内容:
不能超过100个字符



所有留言只代表网友个人观点,不代表本站观点。
请各位遵纪守法并注意语言文明。
新闻搜索
关 键 词:
搜索范围:
全站精华
图文精彩
平面设计QQ群:42326012;技术群:38389792;
本站部分图文内容取自互联网。您若发现有侵犯您著作权行为,请及时告知,我们将在第一时间删除侵权作品、停止继续传播。
Copyright 2006-2007,版权所有 www.fansart.com tc_show@126.com QQ:44550498