Upload multiple images to the specified directory and to the database

Source: Internet
Author: User
Tags format exit chr dsn file upload reset save file trim
There are a lot of articles and original code on the Internet for the example of image uploading. But every time on the forum to browse the post is to see a lot of users of the picture upload headache and trouble. In fact, this problem has also made me feel a headache. Also read a lot of articles and code. Now I write this article is to my favorite piece of code and dynamic Network master of this code optimization, plus I add some code together! Oh, in fact this article is stained with the writing of this Code people (Tao Xiang) and move the net of those masters of Light. :)
Well, don't talk nonsense. Start to build the required environment and data structure!
First create a new folder named photo. (I'm here to upload the picture to this folder.) ) to create a database of names called databases. Then set up a table named Sfile. Interior and exterior design four fields are ID (int), image (varchar), image1 (varchar), image2 (varchar). The database I use here is SQL.
Related files
Register.asp
<title> Documents </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<form name= "Form1" method= "Post" action= "save.asp" >
<table width= "50%" border= "1" cellspacing= "0" cellpadding= "0" align= "center" >
<tr>
&LT;TD width= "20%" ><a href= "#" > Picture upload </a></td>
&LT;TD width= "80%" >
<input type= "text" name= "Myface" >

<input type= "text" name= "Myface1" >

<input type= "text" name= "Myface2" >
(Users do not need to fill in the picture upload automatically generated) </td>
</tr>
<tr>
&LT;TD colspan= "2" align= "Center" >
<input type= "Submit" name= "Submission" value= "submitted" >
<input type= "reset" name= "Submit2" value= "reset" >
</td>
</tr>
</table>
</form>
</body>

Reg_upload.asp
<title></title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<link rel= "stylesheet" href= "Css/style.css" type= "Text/css" >
<body leftmargin= "0" topmargin= "0" >
<br>
<br>
<br>
<table width= "90%" border= "0" align= "center" bgcolor= "#000000" height= "152" cellspacing= "1" >
<tr>
&LT;TD height= "align=" "Center" bgcolor= "#FFFFFF" ><font color= "#FFFF33" ><b><font size= "4" color= "#000000" > Choose pictures </font></b></font></td>
</tr>
<tr>
&LT;TD bgcolor= "#FFFFFF" >
<form name= "form" method= "POST" action= "upfile.asp" enctype= "Multipart/form-data" >
<input type= "hidden" name= "filepath" value= "Photo" >
<input type= "hidden" name= "act" value= "upload" >
<input type= "File" Name= "File1" size= "><br>"
<input type= "File" Name= "File2" size= "><br>"
<input type= "File" Name= "File3" size= ">"

<input type= "Submit" name= "Submit" value= "paste" class= "TL" >
</form>
</td>
</tr>
</table>
</body>

Upfile.asp
<!--#include file= "Upload.inc"-->
<title> File Upload </title>
<body>
<%
Dim arr (3)
Dim upload,file,formname,formpath,icount,filename,fileext,i
Set upload=new upload_5xsoft ' Build upload Object


Formpath=upload.form ("filepath")
"Add (/) to the directory
If Right (formpath,1) <> "/" Then formpath=formpath& "/"
Icount=0
I=0
For every formName in Upload.file ' lists all uploaded files
Set File=upload.file (formName) ' generates a File object

If File.filesize<0 Then
Response.Write "<font size=2> please first select the pictures you want to upload [<a href=# onclick=history.go ( -1) > re-upload </a>]</font> "
Response.End
End If

If file.filesize>100000 Then
Response.Write "<font size=2> picture size exceeded limit [<a href=# onclick=history.go ( -1) > re-upload </a>]</font>"
Response.End
End If
If fileext<> ". gif" and fileext<> ". jpg" Then
Response.Write "<font size=2> file format is not correct [<a href=# onclick=history.go ( -1) > re-upload </a>]</font>"
Response.End
End If

Fileext=lcase (Right (file.filename,4))
Filename=formpath&year (now) &month-&day (now) &hour (now) &minute (now) &second (now) & File. FileName
Arr (i) =filename
I=i+1

If file. Filesize>0 Then ' if FileSize > 0 indicates file data
File. SaveAs server.mappath (filename) ' Save file '
' Response.Write file. Filepath&file. filename& "(" &file. filesize& ") =>" &formPath&File.FileName& "Success!<br>"

Icount=icount+1
End If
Set file=nothing
Next
If InStr (arr (0), ".") =0 and InStr (arr (1), ".") =0 and InStr (Arr (2), ".") =0 Then
Response.Write "<font size=2> please first select the pictures you want to upload [<a href=# onclick=history.go ( -1) > re-upload </a>]</font> "
Response.End
End If
If InStr (arr (0), ".") <>0 Then
Response.Write "<script>opener.document.forms[0].myface.value= '" & Arr (0) & "' </script>"
Else
Response.Write "<script>opener.document.forms[0].myface.value= ' </script>"
End If
If InStr (Arr (1), ".") <>0 Then
Response.Write "<script>opener.document.forms[0].myface1.value= '" & Arr (1) & "' </script>"
Else
Response.Write "<script>opener.document.forms[0].myface1.value= ' </script>"
End If
If InStr (Arr (2), ".") <>0 Then
Response.Write "<script>opener.document.forms[0].myface2.value= '" & Arr (2) & "' </script>"
Else
Response.Write "<script>opener.document.forms[0].myface2.value= ' </script>"
End If
Set Upload=nothing ' Deletes this object

Session ("upface") = "Done"

Htmend icount& "File Upload end!"

Sub Htmend (MSG)
Set upload=nothing
Response.Write "Conn.execute (SQL)
Response.Redirect "Ok.asp"
%>
Oh, the code is a little bit more. Copying the above code into your machine runs is not the result you want! As for the display of the picture I believe we all understand it! I won't have to say more. It is worth mentioning that this code can not only be used for image upload but any format of the file can oh, as for the size of the file can also be defined by yourself Oh! There is only one I defined the upload three if you want to upload more words slightly changed the code on it. Look at the code carefully to find out whether it is very convenient and practical. This is just one of the ways I used to solve this problem. If you have a better way to achieve it, you can also come to tell me email! Learn from each other and make progress together. The first time you write an article may have grammatical problems in its expression. If you have any questions, please email me! My e-mail: hushuowang@163.net,qq:11313112. It's getting late, I'm going to bed! Snoring zzzzzz ....
(especially thanks to Hooke and other experts have been technical support) small Wang Chai in the late 2002/5/26


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.