• 销售:0755-83271919、83271881 服务:0755-83271606、83658105

帮助中心

HELP

如何使用AspUpload组件上传文件

使用AspUpload组件上传文件,使用范例代码如下:

AspUpload组件上传测试


 

 
 
其他表单项

 


<%
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 & " 个文件") & "

"

 ' 指定一个上传的表单文件
 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 "
"
   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 "

"

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


'Response.Write "

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

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

'列出指定的表单内容
Response.Write "
"&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    
%>

  • 投诉/Suggest:13823188098
  • 电话/Tel:0755-83271881
  • 邮箱/Mail:web@ni8.com
  • 地址/Add:深圳市福田区华强南路机械大厦6楼