Use the SA FileUp component to upload multiple files

Source: Internet
Author: User

You can make modifications based on your actual situation, especially the Database Operations Section.
========================================================== ====
Upload multiple files using components of daoxiang's old farmer
See:
Http://bbs.blueidea.com/viewthread.php? Tid = 1249535
========================================================== ============
This example is a hybrid submission of file and form items.

Simple Description:
Although the focus is on the processing page, I think it is necessary to introduce the form project.
This example uploads images from an album.
The groupID is the ID that hides the class passed by the domain.
The albumID indicates the ID of the sub-class passed by the hidden domain.
File1 --> file5 is a file
PhotoTitle1 --> photoTitle5 is the title of an image.
PhotoIntro1 --> photoIntro5 is an overview of the image.
PhotoWidth1 --> photoWidth5 is the width of the image.
PhotoHeight1 --> photoHeigth5 is the height of an image.
PhotoSize1 --> photoSize5 is the image size.

Note: Because I do not have the habit of declaring variables, if you force declaring variables, you have to add them on your own.
========================================================== ===

Submission page: The main code is as follows:
<Form name = "fileForm" method = "post" action = "photo_savephoto.asp" enctype = "multipart/form-data" onSubmit = "return checkForm ();">

<INPUTName = "groupID"Type = "hidden" id = "groupID" value = "<% = groupID %>" size = "10">
<INPUTName = "albumID"Type = "hidden" id = "albumID" value = "<% = albumID %>" size = "10">

Upload Description: You can upload up to five files at the same time, with a title of up to 30 words and a brief introduction of 200 words.

<Table width = "96%" align = "center" cellpadding = "4" cellspacing = "2">
<Tr align = "center" valign = "middle">
<Td align = "left" bgcolor = "# F4CECE" id = "upid"> file 1 </td>
<Td height = "29" align = "left" bgcolor = "# F4CECE" id = "upid"> <INPUTName = "file1" type = "file"Class = "myInput" size = "20">
Width: <INPUTName = "photoWidth1" type = "text"Class = "input-disabled" id = "photoWidth1" size = "5" readonly = "">
High: <INPUTName = "photoHeight1" type = "text"Class = "input-disabled" id = "photoHeight1" size = "5" readonly = "">
Size: <INPUTName = "fileSize1" type = "text"Class = "input-disabled" id = "fileSize1" size = "5" readonly = "">
</Td>
</Tr>
<Tr align = "center" valign = "middle">
<Td colspan = "2" align = "left" bgcolor = "# FDF2F2" id = "upid">
Title: <INPUTName = "photoTitle1" type = "text"Class = "myInput" id = "photoTitle1" size = "40">
<BR>
Introduction: <TEXTAREAName = "photoIntro1"Cols = "60" rows = "5" class = "myInput" id = "photoIntro1"> </TEXTAREA>
</Td>
</Tr>

.......

<Tr align = "center" valign = "middle" bgcolor = "# F4CECE">
<Td height = "24" colspan = "2">
<Input name = "Submit" type = "submit" class = "myButton" value = "Start upload">
<Input name = "Submit2" type = "reset" class = "myButton" value = "re-enter">
</Td>
</Tr>
</Table>
</Form>
The following script is used to check image attributes. Check the image width, height, size, and whether it is an image.

<SCRIPT language = "JavaScript">
<! --
Var upFileSize = <% = upFileSize %>;
// -->
</SCRIPT>

<SCRIPT language = "JavaScript" src = "photo_addphoto.js"> </SCRIPT>

<TABLE width = "98%" border = "0" cellpadding = "0" cellspacing = "0" style = "table-layout: fixed;">
<TR>
<TD height = "1"> </TD>
</TR>
</TABLE>

Photo_addphoto.jsVar firstLoad = true; <br/> var mycount; <br/> function checkForm () <br/>{< br/> var filePath; <br/> var fileNumber = 0; // number of files to be uploaded <br/> for (mycount = 1; mycount <= 5; mycount ++) <br/> if (eval ("fileForm. file "+ mycount + ". value! = ''") // Check the file suffix, title, and description <br/>{< br/> eval ("filePath = fileForm. file "+ mycount + ". value; ") <br/> if (isPic (filePath, mycount) = false) <br/> return false; <br/> if (eval (" fileForm. photoTitle "+ mycount + ". value = ''") | eval ("fileForm. photoTitle "+ mycount + ". value. length> 30 ") <br/>{< br/> alert (" no. "+ mycount +" the title of the photo is null or more than 30 words! "); <Br/> return false; <br/>}< br/> if (eval (" fileForm. photoIntro "+ mycount + ". value = ''") | eval ("fileForm. photoIntro "+ mycount + ". value. length> 200 ") <br/>{< br/> alert (" Number "+ mycount +" Photo introduction is blank or exceeds 200 words! "); <Br/> return false; <br/>}< br/> fileNumber ++; <br/>}< br/> if (fileNumber = 0) <br/>{< br/> alert ("You have not selected any image file! Cannot submit! "); <Br/> return false; <br/>}< br/> return photoInfo (); // Check image attributes, submit the form <br/>}< br/> function photoInfo () <br/> {<br/> // fileForm. submit. disabled = true; <br/> mycount = 0; <br/> photoInfo2 (); // a total of five photoInfo2 () executions <br/> for (var I = 1; I <= 4; I ++) <br/> {<br/> setTimeout ("photoInfo2 ()", I * 500 ); <br/>}< br/> setTimeout ("checkFileSize ()", 3000); <br/> return false; <br/>}< br/> function photoInfo2 () <br/>{< br/> firstLoad = fal Se; <br/> mycount ++; <br/> if (eval ("fileForm. file" + mycount + ". value! = ''") <Br/> eval ("loadPhoto. src = fileForm. file "+ mycount + ". value; "); <br/> else <br/> eval (" fileForm. fileSize "+ mycount + ". value = 'no'; "); <br/>}< br/> function showInfo () <br/>{< br/> eval (" fileForm. photoWidth "+ mycount + ". value = loadPhoto. offsetWidth; "); <br/> eval (" fileForm. photoHeight "+ mycount + ". value = loadPhoto. offsetHeight; "); <br/> eval (" fileForm. fileSize "+ mycount + ". value = loadPhoto. fileSize; "); <br/>}< br/> Function isPic (filePath, mycount) {<br/> var temp; <br/> var ExtList = ".jpg.gif.bmp .png"; <br/> var the_ext = filePath. substr (filePath. lastIndexOf (". ") + 1 ). toLowerCase (); <br/> if (ExtList. indexOf (the_ext) =-1) {<br/> alert ("Number" + mycount + "is not an image. Please select an image file! "); <Br/> return false; <br/>}< br/> return true; <br/>}< br/> function checkFileSize () <br/> {<br/> for (mycount = 1; mycount <= 5; mycount ++) <br/> if (eval ("fileForm. fileSize "+ mycount + ". value ")> upFileSize) // upFileSize comes from photo_addphoto.asp <br/>{< br/> alert (" Number "+ mycount +" file greater than "+ Math. round (upFileSize/1024) + "KB! "); <Br/> return false; <br/>}< br/> fileForm. submit. disabled = true; <br/> fileForm. submit (); // submit the form <br/>}
Tip: you can modify some code before running

====================
Only related input boxes of one file are written here, and the other four are similar.
Note that there are not five restrictions, but I only wrote five.
Among them, photo_addphoto.js checks the image again on the client. If the image size does not exceed the limit, a message is displayed.
This prevents users from being told of noncompliance after waiting patiently.
========================================================== ==========
Processing Page code:

<% @ CODEPAGE = "936" %>
<%
Server. ScriptTimeOut = 5000 '-- set the script timeout value to 5000.
%>
<! -- # Include file = "conn. asp" -->
<%
Set oFileUp = Server. CreateObject ("SoftArtisans. FileUp") '-- create SA FileUp Object

'Ofileup. Path = Server. MapPath ("/upfile/") '-- I want to define the file name here, so the Path attribute is not used. Note that this sentence has been commented out.

StrRestrictBy = "" '-- file authentication method, which can be extension or ContentType

ICount = 0' -- variable for counting the number of file uploads

FormPath = "upfile/" 'file storage location

'----------- Check whether you have the permission to upload at this location --------- this is omitted.
GroupID = trim (oFileUp. form ("groupID "))
AlbumID = trim (oFileUp. form ("albumID "))
'----------- Check permission completed ------

If errMsg = "" then' ---- if there is no error
For Each strFormElement In oFileUp. Form
If IsObject (oFileUp. Form (strFormElement) then' is a file
If Not oFileUp. Form (strFormElement). IsEmpty then' -- the file is Not empty.
FlagOK = 1
'-- If the file or file is larger than the limit, set the error message.
If oFileUp. Form (strFormElement). TotalBytes <100 Then
FlagOK = 0
ElseIf oFileUp. Form (strFormElement). TotalBytes> upFileSize Then
FlagOK = 0
ErrMsg = errMsg + "file:" & oFileUp. Form (strFormElement). UserFileName & "greater than" & upFileSize \ 1024 & "KB! <Br>"
Else
Strpolicfilename = mid (oFileUp. Form (strFormElement). UserFileName, Rev (oFileUp. Form (strFormElement). UserFileName, "\") + 1) 'get the file name
StrExtension = Mid (str1_filename, limit Rev (str1_filename, ".") 'Get the extension
If strRestrictBy = "extension" then' -- the verification method is extension
'Strpolicfilename = oFileUp. Form (strFormElement). invalid filename
'====== Check the suffix ====
Select Case LCase (strExtension)
Case ". jpg", ". gif", ". bmp", ". png"

Case Else
FlagOK = 0
OFileUp. Form (strFormElement). Delete
Response. Write ("<B> error: </B> the file with the extension <I>" & strExtension & "</I> cannot be uploaded. <BR> ")
End Select
Else' -- the verification method is MIME type.
StrContentType = oFileUp. Form (strFormElement). ContentType
Select Case LCase (strContentType)
Case "image/gif", "image/jpeg", "image/pjpeg"
Case Else
FlagOK = 0
OFileUp. Form (strFormElement). Delete
Response. Write ("<B> error: </B> the file whose MIME type is <I>" & strContentType & "</I> cannot be uploaded. <BR> ")
End Select
End If '-- end if Verification Method
End If 'end if file size judgment

If flagOK = 1 then' If the file passes the check, save the file and insert the database record
Randomize
RanNum = int (900 * rnd) + 100
Filename = year (now () & month (now () & day (now () & hour (now () & minute (now ()) & second (now () & ranNum & LCase (strExtension)
OFileUp. Form (strFormElement). SaveInVirtual formPath & filename 'allows the file name to be unique and saves the file. Here the SaveInVirtual method is used.

'-- File path on the output Server
Response. Write oFileUp. Form (strFormElement). ServerName & ": ServerName <BR>"

'-- File path of the output Client
Response. Write "<BR> <B> file: </B>" & oFileUp. Form (strFormElement). UserFileName & "<BR>"

'-- Output the file size
Response. Write "<B> size: </B>" & oFileUp. Form (strFormElement). TotalBytes & "<BR>"
'=== Add file information to the database ====
MyIndex = right (strFormElement, 1) '-- obtains the object sequence number. For example, if file1 is obtained, it is obtained as 1 and file2 is obtained as 2.
Temp_photoTitle = oFileUp. form ("photoTitle" + myIndex) '-- these four rows obtain the corresponding title, description, width, and height.
Temp_photoIntro = oFileUp. form ("photoIntro" + myIndex)
Temp_photoWidth = oFileUp. form ("photoWidth" + myIndex)
Temp_photoHeight = oFileUp. form ("photoHeight" + myIndex)
'==== Check input. If it is null, the initial value is =
Temp_photoTitle = replace (trim (temp_photoTitle ),"'","''")
If temp_photoTitle = "" then
Temp_photoTitle = "not filled"
End if
Temp_photoIntro = replace (trim (temp_photoIntro ),"'","''")
If temp_photoIntro = "" then
Temp_photoIntro = "not filled"
End if
If temp_photoWidth = "" or not IsNumeric (temp_photoWidth) then
Temp_photoWidth = 160
End if
If temp_photoHeight = "" or not IsNumeric (temp_photoHeight) then
Temp_photoHeight = 120
End if
'=== Insert a database ===
FileSize = oFileUp. Form (strFormElement). TotalBytes
SQL = "insert into TBL_PHOTO (albumID, groupID, userName, addTime, photoFilename, photoTitle, photoIntro, photoClick, photoSize, photoWidth, photoHeight, locked, viewPassword) values ("& albumID &", "& groupID &", '"& session (" userName ") &"', '"& Now ()&"', '"& filename &"', '"& temp_photoTitle &"', '"& temp_photoIntro &"', 1, "& FileSize &", "& temp_photoWidth &", "& temp_photoHeight &", 'No ','')"
Conn.exe cute SQL
SQL = "update TBL_ALBUM set photoCount = photoCount + 1 where albumID =" & albumID
Conn.exe cute SQL
SQL = "update TBL_GROUP set photoCount = photoCount + 1 where groupID =" & groupID
Conn.exe cute SQL
'=== Output successful upload information ===
ICount = iCount + 1
End If

Else
Response. Write strFormElement & "the object is empty! "
End If '-- end if object is empty
End If '-- end if is a file
Next
Set oFileUp = Nothing 'delete this object
End if '-- end if has no error message

Response. write "<br>" & iCount & "File Upload is complete! "
Response. write "<br> <a href = 'photo _ listphoto. asp? AlbumID = "& albumID &" '> <B> back to album </B> </a>"
'==== If there is an error, the output error message ====
If errMsg <> "then
Response. write "<br>" & errMsg
Response. write "<INPUT type = 'button 'onclick = 'History. go (-1) 'value =' Return 'class = 'myinput'>"
End if
Conn. close
Set conn = nothing
%>

========================================================== ==============
After reading the instance, I will give a brief introduction to the attributes and methods of SA FileUP, so that you will not feel dizzy in your first contact.
These are commonly used. Examples and comments are written according to my understanding.
If you find any problems in the application, please point out. Thank you.

Method for creating a SA FileUp object:
Set oFileUp = Server. CreateObject ("SoftArtisans. FileUp ")

Method to retrieve all items in the form:
For Each strFormElement In oFileUp. Form
You can use oFileUp. Form (strFormElement) to reference each object.
Note: If you select multiple drop-down boxes, use oFileUp. FormEx (strFormElement)
You can traverse it like this.
For Each strSubItem In oFileUp. FormEx (strFormElement)
Response. Write (strSubItem & "<BR> ")
Next
Next

ContentType attributes:
OFileUp. Form (strFormElement). ContentType
The MIME type of the file can be obtained.

IsEmpty attributes
OFileUp. Form (strFormElement). IsEmpty
You can see whether an invalid file is specified.

MaxBytes attributes
OFileUp. Form (strFormElement). MaxBytes = 30000
The size of the specified file. The unit is Byte. If it is exceeded, only the size specified by MaxBytes is stored. The rest are discarded.

ServerName attributes
OFileUp. Form (strFormElement). ServerName
You can get the complete path to save the file to the server.

Invalid filename attribute
OFileUp. Form (strFormElement). invalid filename
You can get the file name of the client. Note that it is only the file name. It is useless because an error is reported. Haha.

UserFilename attributes
OFileUp. Form (strFormElement). UserFileName
Obtain the complete path of the client file. You can output it to the user.

TotalBytes attributes
OFileUp. Form (strFormElement). TotalBytes
The file size is measured in bytes.

SaveInVirtual (PATH) Method
OFileUp. Form (strFormElement). SaveInVirtual "upfile /"
OFileUp. Form (strFormElement). SaveInVirtual "upfile/aa. abc"
If only the path is specified, the original file name is retained; otherwise, the file name is saved as specified.
The server administrator can disable all other methods, but this will definitely stay.

SaveAs (File Name) Method
OFileUp. Form (strFormElement). SaveAs "C: \ aa \ a. tmp"
If no Path is specified but the file name is specified, the Path specified by the Path attribute is used.
The Path attribute will be introduced later.

Save Method
OFileUp. Path = "D: \ wwwroot \ abc \ upfile \"
Note that the path must be a real path. You can use Server. MapPath to convert the virtual path.
OFileUp. Form (strFormElement). Save
The file name cannot be specified.

Note: The Path attribute must be placed before any form item is mentioned.
Set oFileUp = Server. CreateObject ("SoftArtisans. FileUp ")
. The premise is that you use it.

Delete (file name, optional)
OFileUp. Form (strFormElement). Delete
Delete a file from the server. If no file name is specified, the current file will be deleted.
If specified, it must be the complete path of the file.

Flush Method
OFileUp. Flush
When you don't want to save anything, you can use it to discard all input streams.

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.