|
|
| ตัวอย่างการ
Upload ไฟล์ |
ตัวอย่างนี้ผมไม่ได้ทำขึ้นเองได้จากที่อื่นมาเพราะโค๊ชยาวมากจะเขียนคงไม่ใหวแต่ผมทำใหม่ดูแล้วเข้าใจง่ายมากขึ้น
คุณสามารถ upload file .jpg .gif .zip และอื่นๆ อีกมาก ลองทำตามเลยนะครับ |
| วิธีทำ |
| 1.สร้างฐานข้อมูล
Access ชื่อ Upload.mdb
|
ไฟล์
Upload.mdb สร้างตารางชื่อ upload
ตาราง
upload |
ชื่อข้อมูล |
ชนิดข้อมูล |
id |
Autonumber |
topic |
Text |
fliename |
Text |
date |
date/time |
|
| 2.
สร้าง Flie ชื่อ upload.asp
รายละเอียดมีดังนี้ครับ |
<form
action="new_vote2.asp" method="post"
enctype="multipart/form-data" name="form1">
<table width="74%" border="0"
align="center" cellspacing="1">
<tr>
<td width="12%"><font size="2"
face="Microsoft Sans Serif, MS Sans Serif, sans-serif">หัวข้อ</font></td>
<td width="88%"><font size="2"
face="MS Sans Serif">
<input name="topic" type="text"
id="topic" size="30">
</font></td>
</tr>
<tr>
<td><font size="2" face="Microsoft
Sans Serif, MS Sans Serif, sans-serif">ไฟล์</font></td>
<td><font size="2" face="MS
Sans Serif">
<input name="blob" type="file"
id="blob" size="40">
</font></td>
</tr>
<tr>
<td><font size="2" face="MS
Sans Serif"> </font></td>
<td><font size="2" face="MS
Sans Serif">
<input type="submit" name="Submit"
value="อัฟโหลด">
</font></td>
</tr>
</table>
</form> |
|
| 3.
สร้าง Flie ชื่อ upload2.asp
รายละเอียดมีดังนี้ครับ |
<%
Response.Expires=0
Response.Buffer = TRUE
Response.Clear
byteCount = Request.TotalBytes
RequestBin = Request.BinaryRead(byteCount)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")
BuildUploadRequest RequestBin
topic = UploadRequest.Item("topic").Item("Value")
contentType = UploadRequest.Item("blob").Item("ContentType")
filepathname = UploadRequest.Item("blob").Item("FileName")
filename = Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))
filename1 =Right(filename,4)
if filename="" then
topic=Request.ServerVariables("REMOTE_ADDR")
date=now()
else
mdate=replace(date(),"/","")
mtime=replace(time(),":","")
ntime=left(mtime,5)
newname=mdate+ntime
value = UploadRequest.Item("blob").Item("Value")
Set ScriptObject = Server.CreateObject("Scripting.FileSystemObject")
Set MyFile = ScriptObject.CreateTextFile(Server.mappath(newname&filename1))
For i = 1 to LenB(value)
MyFile.Write chr(AscB(MidB(value,i,1)))
Next
MyFile.Close
Sub
BuildUploadRequest(RequestBin)
'Get the boundary
PosBeg = 1
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
boundaryPos = InstrB(1,RequestBin,boundary)
'Get all data inside the boundaries
Do until (boundaryPos=InstrB(RequestBin,boundary &
getByteString("--")))
'Members variable of objects are put in a dictionary
object
Dim UploadControl
Set UploadControl = CreateObject("Scripting.Dictionary")
'Get an object name
Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))
Pos = InstrB(Pos,RequestBin,getByteString("name="))
PosBeg = Pos+6
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))
PosBound = InstrB(PosEnd,RequestBin,boundary)
'Test if object is of file type
If PosFile<>0 AND (PosFile<PosBound) Then
'Get Filename, content-type and content of file
PosBeg = PosFile + 10
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
'Add filename to dictionary object
UploadControl.Add "FileName", FileName
Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
PosBeg = Pos+14
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
'Add content-type to dictionary object
ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
UploadControl.Add "ContentType",ContentType
'Get content of object
PosBeg = PosEnd+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
Value = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
Else
'Get content of object
Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
PosBeg = Pos+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
End If
'Add content to dictionary object
UploadControl.Add "Value" , Value
'Add dictionary object to main dictionary
UploadRequest.Add name, UploadControl
'Loop to next object
BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
Loop
End
Sub
'String
to byte string conversion
Function getByteString(StringStr)
For i = 1 to Len(StringStr)
char = Mid(StringStr,i,1)
getByteString = getByteString & chrB(AscB(char))
Next
End Function
'Byte
string to string conversion
Function getString(StringBin)
getString =""
For intCount = 1 to LenB(StringBin)
getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function
newfilename=newname+filename1
date=now()
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "DRIVER={Microsoft Access Driver (*.MDB)};DBQ="
& Server.Mappath("upload.mdb")
sql = "SELECT * FROM [upload]"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, conn, 3, 3
On Error Resume Next
rs.Addnew
rs("topic")=topic
rs("date")=date
rs("fliename")=newfilename
rs.Update
rs.close
conn.Close
end if
%>
|
|
ก็จะมีอยู่สองไฟล์นี้ครับ
ลองทำดูนะครับ อัฟได้จริงๆ ตัวอย่าง หน้าสติ๊กเกอร์โหวดผมไงครับแบบนี้เลย ดึงออกมาแสดงก็สามารถ
ดูทริปที่ผ่านมา คลิ๊กที่นี่เลยครับ
โค๊ชจาก ชัชวาล มาไกล webmater ohlanla.com
|
|
|