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

zhushican | 建站教程 | 2014年7月14日

使用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

[阅读全文]
5次浏览 0条评论 AspUpload  组件  上传文件  
回顶部