A function without component upload file

Source: Internet
Author: User
Tags format exit file size return
function | upload | no component <%
'''''==============================
' Function name: upfile
' Function: Use ' transformation upload component ' to upload files to server
' Parameter: File1 file object
' Savepath file to save relative path, such as '. /"record at the top level," "the same directory
' MaxSize allows the maximum file size to be uploaded, in kilobytes KB. is 0 Unlimited.
' Savetype allows the type of file to be uploaded, 0 unrestricted, restricted format. such as. Jpg|. bmp|. Zip
' Return value: Returns the upload information, but also can set the return value according to the need
' Premise: Set upload=new upload_5xsoft ' Build upload Object
' Set File1=upload.file (' file1 ') ' generates a File object
' Designer:suercool
function Upfile (file1,savepath,maxsize,savetype)
If File1.filename= "" and file1.filesize<=0 Then
upfile= "<script language= ' JavaScript ' >alert (' file does not exist! ') </script> "
Exit function
End If

If maxsize<> "0" and FILE1.FILESIZE&GT;CLNG (maxsize) *1024 Then
upfile= "<script language= ' JavaScript ' >alert (' file size exceeds the limit, maximum upload only" & CStr (maxsize) & "KB files! ') </script> "
Exit function
End If

Dim Filename,filetype
Filename=file1.filename
Filetype=getfiletype (filename)

If savetype<> "0" Then
Dim Arrtype,i,foundtype
Arrtype=split (Savetype, "|")
Foundtype=false
For i = 0 To UBound (arrtype)
If LCase (Arrtype (i)) =filetype then
Foundtype=true
Exit For
End If
Next
If not Foundtype then
upfile= "<script language= ' JavaScript ' >alert (' file format is not accurate, only allowed to upload ' & savetype & ' Format Files! ') </script> "
Exit function
End If
End If
Randomize ()
Filepath=savepath&year (now) &month-&day (now) &hour (now) &minute (now) &second (now) & Int (rnd*9999) &filetype
File1.saveas Server.MapPath (filepath)
If err.description<> "" Then
upfile= "<script language= ' JavaScript ' >alert (' unknown error, upload failed, please try again! ') </script> "
Err.Clear
Else
upfile= "<script language= ' JavaScript ' >alert (' upload success! ') +filepath+ "');</script>"
End If
Set file1=nothing
End Function
''''''''===========
' Function name: Getfiletype
' Function: Get the type of file
' parameter: filename filename
' Return value: File type, no type return '
' Designer:suercool
function Getfiletype (filename)
Dim i
For i= (len (filename)-1) to 1 step-1
If mid (filename,i,1) = "." Then
Getfiletype=lcase (Mid (Filename,i))
Exit function
End If
Next
Getfile= ""
End Function
%>

Instance (upfile3.asp):
<!--#include file= "Upload_5xsoft.inc"-->
<title>Upfile</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>
<form action= "upfile3.asp" name= "Frmupfile" id= "Frmupfle" enctype= "Multipart/form-data" method=post>
<input type=file name= "File1" >
<input type=submit value= "Upload" name=subupfile>
<input type=hidden name= "upfilename" value= "" >
<% set Upload=new upload_5xsoft ' Set up an uploaded object
If Upload.form ("subupfile") = "Upload" Then
Set File1=upload.file ("File1")
Dim re
Savetype= ". Jpg|. Htm|. Bmp| "
Re=upfile (File1, "", "0", Savetype)
Response.Write RE
End If
%>
</form>
</body>


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.