ASP picture File upload code full version

Source: Internet
Author: User
Tags file size file upload lowercase save file trim


<!--#include file= "Upload_wj.inc"-->
<style>
Td{font-size:9pt;line-height:120%;color: #353535}
body{font-size:9pt;line-height:120%}

a:link {color: #000000; Text-decoration:none}
a:visited {color: #000000; Text-decoration:none}
a:active {color: #000000; Text-decoration:none}
a:hover {color: #336699; text-decoration:none; position:relative; right:0px; top:1px}
</style>
<%
Set Upload=new Upload_file
If Upload.form ("act") = "UploadFile" Then
Filepath=trim (Upload.form ("filepath"))
Filelx=trim (Upload.form ("Filelx"))

I=0
For each formName in upload. File
Set File=upload. File (FormName)

Fileext=lcase (file. Fileext) ' The resulting file extension does not contain.
If File.filesize<100 Then
Response.Write "<span style=" "Font-family: Song body; Font-size:9pt "" > Please select the file you want to upload first! [<a href=# Onclick=history.go ( -1) > re-upload </a>]</span> "
Response.End
End If
if (filelx<> "SWF") and (filelx<> "JPG") and (filelx<> "GIF") then
Response.Write "<span style=" "Font-family: Song body; Font-size:9pt "" > the file type cannot be uploaded! [<a href=# Onclick=history.go ( -1) > re-upload </a>]</span> "
Response.End
End If
If filelx= "SWF" then
If fileext<> "SWF" then
Response.Write "<span style=" "Font-family: Song body; Font-size:9pt "" > can only upload the SWF format Flash file! [<a href=# Onclick=history.go ( -1) > re-upload </a>]</span> "
Response.End
End If
End If
If filelx= "JPG" or filelx= "GIF" then
If fileext<> "gif" and fileext<> "JPG" then
Response.Write "<span style=" "Font-family: Song body; Font-size:9pt "" > can only upload images in jpg or GIF format! [<a href=# Onclick=history.go ( -1) > re-upload </a>]</span> "
Response.End
End If
End If
If filelx= "SWF" then
If File.filesize> (500*1024) Then
Response.Write "<span style=" "Font-family: Song body; Font-size:9pt "" > Maximum can upload 500K of flash file! [<a href=# Onclick=history.go ( -1) > re-upload </a>]</span> "
Response.End
End If
End If
If filelx= "JPG" or filelx= "GIF" then
If File.filesize> (100*1024) Then
Response.Write "<span style=" "Font-family: Song body; Font-size:9pt "" > can only upload 100K of picture File! [<a href=# Onclick=history.go ( -1) > re-upload </a>]</span> "
Response.End
End If
End If

Randomize
Rannum=int (90000*RND) +10000
Filename=filepath&year (now) &month-&day (now) &hour (now) &minute (now) &second (now) & rannum& "." &fileext
%>
<%
If file. Filesize>0 Then ' if FileSize > 0 indicates file data
' File. SaveAs server.mappath (filename) ' Save file '
File. SaveToFile Server.MapPath (FileName)
' Response.Write file.  filename& "Upload success!" <br> "
"Response.Write" New file name: "&FileName&" <br> "
The ' Response.Write ' new filename has been copied to the desired location to close the window! "
If filelx= "SWF" then
Response.Write "<script>window.opener.document." &upload.form ("FormName") & ". Size.value= '" &int (file. filesize/1024) & "K ' </script>"
End If
Response.Write "<script>window.opener.document." &upload.form ("FormName") & "." &upload.form ("Editname") & ". Value= '" &FileName& "' </script>"
%>
<%
End If
Set file=nothing
Next
Set upload=nothing
End If
%>
<script language= "JavaScript" >
Window.alert ("File upload success!) Please do not modify the generated link address! ");
Window.close ();
</script>

Upload_wj.inc File Code

<%

' File properties: For example, upload file as C:myfiledoc.txt
' filename    filename        string      "Doc.txt"
' filesize    file size      value         1210
' filetype    file type      string     ' Text/plain '
' fileext     file name extension    string     ' txt '
' filepath    File original path    string     "C:myfile"
' note when used:
' because Scripting.Dictionary is case-sensitive, So in the page and ASP page of the project names to the same size
' write, if people are accustomed to using uppercase or lowercase, in order to prevent errors, you can put
' sformname = Mid (Sinfo,ifindstart,ifindend-ifindstart)
Change to
' (lowercase) sformname = LCase (Mid (Sinfo,ifindstart,ifindend-ifindstart))
' (uppercase) Sformname = UCase (Mid (Sinfo , Ifindstart,ifindend-ifindstart))
' **********************************************************************
'----------------------------------------------------------------------
Dim oupfilestream

Class Upload_file

Dim form,file,version

Private Sub Class_Initialize
' Define variables
Dim requestbindate,sstart,bcrlf,sinfo,iinfostart,iinfoend,tstream,istart,ofileinfo
Dim ifilesize,sfilepath,sfiletype,sformvalue,sfilename
Dim ifindstart,ifindend
Dim iformstart,iformend,sformname
' Code starts
version= "No component upload class Version 0.96"
Set Form = Server.CreateObject ("Scripting.Dictionary")
Set File = Server.CreateObject ("Scripting.Dictionary")
If Request.TotalBytes < 1 then Exit Sub
Set tstream = Server.CreateObject ("ADODB.stream")
Set oupfilestream = Server.CreateObject ("ADODB.stream")
Oupfilestream.type = 1
Oupfilestream.mode = 3
Oupfilestream.open
Oupfilestream.write Request.BinaryRead (request.totalbytes)
Oupfilestream.position=0
Requestbindate = Oupfilestream.read
Iformend = Oupfilestream.size
Bcrlf = ChrB (+) & ChrB (10)
' Get the separator between each item
Sstart = MidB (requestbindate,1, InStrB (1,REQUESTBINDATE,BCRLF)-1)
IStart = LenB (Sstart)
Iformstart = istart+2
' Explode project
Todo
Iinfoend = InStrB (Iformstart,requestbindate,bcrlf & Bcrlf) +3
Tstream.type = 1
Tstream.mode = 3
Tstream.open
Oupfilestream.position = Iformstart
Oupfilestream.copyto Tstream,iinfoend-iformstart
tstream.position = 0
Tstream.type = 2
Tstream.charset = "gb2312"
Sinfo = Tstream.readtext
' Get form Item name
Iformstart = InStrB (iinfoend,requestbindate,sstart)-1
Ifindstart = InStr (22,sinfo, "name=" "", 1) +6
Ifindend = InStr (Ifindstart,sinfo, "" "", 1)
Sformname = Mid (Sinfo,ifindstart,ifindend-ifindstart)
' If it's a file
If InStr (45,sinfo, "filename=" "", 1) > 0 Then
Set ofileinfo= new FileInfo
' Get file properties
Ifindstart = InStr (Ifindend,sinfo, "filename=" "", 1) +10
Ifindend = InStr (Ifindstart,sinfo, "" "", 1)
sFileName = Mid (Sinfo,ifindstart,ifindend-ifindstart)
Ofileinfo.filename = GetFileName (sfilename)
Ofileinfo.filepath = GetFilePath (sfilename)
Ofileinfo.fileext = Getfileext (sfilename)
Ifindstart = InStr (Ifindend,sinfo, "Content-type:", 1) +14
Ifindend = InStr (IFINDSTART,SINFO,VBCR)
Ofileinfo.filetype = Mid (Sinfo,ifindstart,ifindend-ifindstart)
Ofileinfo.filestart = Iinfoend
Ofileinfo.filesize = Iformstart-iinfoend-2
Ofileinfo.formname = Sformname
File.add Sformname,ofileinfo
Else
' If it is a form item
Tstream.close
Tstream.type = 1
Tstream.mode = 3
Tstream.open
Oupfilestream.position = Iinfoend
Oupfilestream.copyto tstream,iformstart-iinfoend-2
tstream.position = 0
Tstream.type = 2
Tstream.charset = "gb2312"
Sformvalue = Tstream.readtext
Form. ADD Sformname,sformvalue
End If
Tstream.close
Iformstart = iformstart+istart+2
' If you get to the end of the file, quit.
Loop until (iformstart+2) = Iformend
Requestbindate= ""
Set Tstream = Nothing
End Sub

Private Sub class_terminate 
  ' Clear variables and to
  if not request.totalbytes<1 then
     Oupfilestream.close
    set oupfilestream =nothing
    End If
  Form.removeall
  File.removeall
  set form=nothing
  set file=nothing
End Sub
&NBSP;&N Bsp
  Get file path
Private function GetFilePath (fullpath)
  If fullpath <> "Then
  &nb Sp GetFilePath = Left (Fullpath,instrrev (FullPath, ""))
    Else
    getfilepath = ""
  End If
End Function
 
' get filename
Private function GetFileName (fullpath)
  If Fullpat H <> "" Then
    GetFileName = Mid (Fullpath,instrrev (FullPath, "") +1)
    Else
    getfilename = "
  end If
End Function

' Get extension
Private function Getfileext (fullpath)
If fullpath <> "" Then
Getfileext = Mid (Fullpath,instrrev (FullPath, ".") +1)
Else
Getfileext = ""
End If
End Function

End Class

' File attribute Class
Class FileInfo
Dim formname,filename,filepath,filesize,filetype,filestart,fileext
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
filestart= 0
FormName = ""
FileType = ""
Fileext = ""
End Sub

' Save File Method
Public Function SaveToFile (fullpath)
Dim ofilestream,errorchar,i
Savetofile=1
If fileext<> "gif" and fileext<> "jpg" and fileext<> "swf" then Exit Function ' detect extension
If trim (fullpath) = "" or right (fullpath,1) = "/" Then Exit function
Set Ofilestream=createobject ("ADODB.stream")
Ofilestream.type=1
Ofilestream.mode=3
Ofilestream.open
Oupfilestream.position=filestart
Oupfilestream.copyto ofilestream,filesize
Ofilestream.savetofile fullpath,2
Ofilestream.close
Set ofilestream=nothing
Savetofile=0
End Function
End Class
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.