HOME > ASP > ASP Forum > ช่วยด้วยค้าบบ ผมมีปัญหามากๆ T___T Set fs อย่างโคด้ข้างบนเนี้ยอะครับ มันเป็นโคด้อัพโหลด แต่มมันให้อัพได้เฉพาะไฟล์ .GIF เราจะทำยังไงอ่าคับ ให้เวลาคนใส่ไฟล์ Jpg หรือ Gif
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set uploadfile = fs.CreateTextFile(Server.mappath(pathupload&memberid&"s.gif"))
uploadfile.Write(filevalue)
uploadfile.Close
set uploadfile = Nothing
Set uploadfile = fs.CreateTextFile(Server.mappath(pathupload&memberid&"m.gif"))
uploadfile.Write(filevalue2)
uploadfile.Close
set uploadfile = Nothing
set fs=Nothing
ต่อไปนี้คือโค้ดของ
inc_upload.asp
-------------------------------------------------------------------------------------------------------------
<%
'-----------------------------------------------------------------------------
'Muli File's upload Created by Bhushan Paranjpe
'-----------------------------------------------------------------------------
Sub BuildUploadRequest(RequestBin)
'Get the boundary
PosBeg = 1
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
if PosEnd = 0 then
Response.Write "<b>Form was submitted with no ENCTYPE=""multipart/form-data""</b><br>"
Response.Write "Please correct the form attributes and try again."
Response.End
end if
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))
FileName = Mid(FileName,InStrRev(FileName,"\")+1)
'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 = FileName
ValueBeg = PosBeg-1
ValueLen = 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))
ValueBeg = 0
ValueEnd = 0
End If
'Add content to dictionary object
UploadControl.Add "Value" , Value
UploadControl.Add "ValueBeg" , ValueBeg
UploadControl.Add "ValueLen" , ValueLen
'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
Function UploadFormRequest(name)
on error resume next
if UploadRequest.Item(name) then
UploadFormRequest = UploadRequest.Item(name).Item("Value")
end if
End Function
%>
for AF_i = 0 to UploadRequest.Count - 1
AF_curKey = AF_keys(AF_i)
if UploadRequest.Item(AF_curKey).Item("FileName") <> "" then
AF_valueLen = UploadRequest.Item(AF_curKey).Item("ValueLen")
select case Right(UploadRequest.Item(AF_curKey).Item("FileName"), 3)
'Allow these file extensions to be uploaded
case "gif", "jpg", "png", "GIF", "JPG", "PNG"
'Warning if file size is 0 byte
if AF_valueLen = 0 then
Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
Response.Write "Filename: " & Trim(AF_curPath) & UploadRequest.Item(AF_curKey).Item("FileName") & "<br>"
Response.Write "File does not exists or is empty.<br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
Response.End
end if
'Warning if file size is more than max one
if AF_valueLen > imgMaxFileSize then
Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
Response.Write "Filename: " & Trim(AF_curPath) & UploadRequest.Item(AF_curKey).Item("FileName") & "<br>"
Response.Write "File size (" & FormatNumber(AF_valueLen,0) & " Bytes) is more than max file size (" & FormatNumber(imgMaxFileSize,0) & " Bytes).<br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
Response.End
end if
case else
Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
Response.Write "Filename: " & Trim(AF_curPath) & UploadRequest.Item(AF_curKey).Item("FileName") & "<br>"
Response.Write "File extensions should only be ""gif, jpg, png"".<br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
Response.End
end select
' else
'Warning if file is blank
' Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
' Response.Write "Filename: " & Trim(AF_curPath) & UploadRequest.Item(AF_curKey).Item("FileName") & "<br>"
' Response.Write "File does not exists or is empty.<br>"
' Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
' Response.End
end if
next
'------
Dim numFileName
'Assign initial file number
numFileName = 1
for AF_i = 0 to UploadRequest.Count - 1
AF_curKey = AF_keys(AF_i)
'Save all uploaded files
if UploadRequest.Item(AF_curKey).Item("FileName") <> "" then
AF_value = UploadRequest.Item(AF_curKey).Item("Value")
AF_valueBeg = UploadRequest.Item(AF_curKey).Item("ValueBeg")
AF_valueLen = UploadRequest.Item(AF_curKey).Item("ValueLen")
'Create a Stream instance
Dim AF_strm1, AF_strm2
Set AF_strm1 = Server.CreateObject("ADODB.Stream")
Set AF_strm2 = Server.CreateObject("ADODB.Stream")
'Create and Write to a File
AF_curPath = Request.ServerVariables("PATH_INFO")
AF_curPath = Trim(Mid(AF_curPath,1,InStrRev(AF_curPath,"/")) & "")
if Mid(AF_curPath,Len(AF_curPath),1) <> "/" then
AF_curPath = AF_curPath & "/"
end if
on error resume next
'------
'Declare variable to store folder name and save to file and store file names To array variables
Dim imgFolderName, numFile
imgFolderName = "\upload\"
'Assign file name number as yyyymdhms + running number
numFile = Year(Now) & Month(Now) & Day(Now) & Hour(Now) & Minute(Now) & Second(Now) & numFileName & Right(LCase(UploadRequest.Item(AF_curKey).Item("FileName")),4)
if err then
Response.Write "<B>An error has occured saving uploaded file!</B><br><br>"
Response.Write "Filename: " & Trim(AF_curPath) & UploadRequest.Item(AF_curKey).Item("FileName") & "<br>"
Response.Write "Maybe the destination directory does not exist, or you don't have write permission.<br>"
Response.Write "Please correct and <A HREF=""javascript:history.back(1)"">try again</a>"
err.clear
Response.End
end if
end if
next
'------
'Display file names to browser or Save to file names to your database
Dim sFileName(1), i
for AF_i = 0 to UploadRequest.Count - 1
If imgFileName(AF_i) <> "" Then
'Assign file name to array variables, 1 is Max number of file upload that you can change
for i = 0 to 1
If sFileName(i) = "" Then
sFileName(i) = imgFileName(AF_i)
exit for
End If
next
End If
next