如何使用AspUpload组件上传文件?

作者: zhushican 分类: 建站教程 发布时间: 2014年7月14日 次浏览 0条评论

使用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
  htm = htm& chinese2unicode("错误: 文件过大!")
  Response.end
 Else
  If Err <> 0 Then
   htm = htm& chinese2unicode("发生错误:")
   htm = htm& chinese2unicode(Err.Description)
   response.end
  End If
 End If
 'htm = htm& 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
   htm = htm& chinese2unicode("错误: 文件类型不正确!")
   htm = htm& "<br>"
   htm = htm& chinese2unicode("只允许:"&AllowExt)
   ' 删除内存中的临时文件,以释放内存或硬盘空间(还可用Copy、Move两个指令)
   File.Delete
  ' 检测是否存在文件
  elseif Upload.FileExists(uploadDir & "\\" & Filename) Then
   File.SaveAs uploadDir & "\\" & Filename
   htm = htm& chinese2unicode("已覆盖存在相同文件名的文件: ") & File.Path
  ' 保存文件
  else
   File.SaveAs uploadDir & "\\" & Filename
   htm = htm& chinese2unicode("文件已保存到: ") & File.Path
  end If
 Else
  htm = htm& chinese2unicode("错误: 您并没有选择文件!")
 End If
 htm = htm& "<br><br>"
 '' 批量上传文件,去掉注释即可用。
 For Each File in Upload.Files
   'File.SaveAs uploadDir & "\\" & File.FileName
   'htm = htm& chinese2unicode("文件已保存到: ") & File.Path & "<br>"
 Next

'htm = htm& "<br><br>Files:<br>"
For Each File in Upload.Files
 'htm = htm& File.Name & "= " & File.Path & " (" & File.Size &" bytes)<br>"
Next
'' 列出其他表单内容(必须执行Upload.Save()后才有效)
For Each Item in Upload.Form
 htm = htm& Item.Name & " = " & Item.Value & "<br>"
Next
'列出指定的表单内容
htm = htm& "<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
 htm = htm& Str_unicode
end function   
%>

本文出自 老域名查询 ,转载时请注明出处及相应链接。

本文永久链接: http://www.sc8323.com/2014/311.html

回顶部