代表一个控制的Dictionary 对象包含着下面的(key, item) 对: "Value", String or binary content "FileName", Name of uploaded file "ContentType", ContentType of uploaded file
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 如果是控制是文件类控制,就将路径和文件名进行分解,并将他们填加到控制的dictionary 对象中。分解后的文件名是一个单字节字符串,要将它转换成双字节字符串才能作为variant字符串变量使用。这通过最后定义的getString()方法来实现:
’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
将内容加入dictionary对象中。将key设置成 " Value ",那么item 就是内容。根据控制类型的不同,内容可以是字符串或二进制数据。
’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
字节-字符串转换函数
下面是将双字节字符串转换成单字节字符串的函数。
’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 下面是将字符串转换成单字节字符串的函数,它用来格式化InstrB函数的自变量。
’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
下面的第一个例子为只是将客户端的文件上传到服务端的例子 第二个例子为将文件内容保存入数据库中。 文件fupload.asp <% dim ResultHTML 'Some value greater than default of 60s (According to upload size.) 'The maximum speed is about 100kB/s for IIS4, P200 and local upload, 4kB/s for modem users. Server.ScriptTimeout = 400
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then 'Request method must be "POST" for get the fields ' BeginTimer 'Starts timer. '************************************************* Main Upload - start Dim Fields ' on error resume next 'Set upload limit to 10M UploadSizeLimit = 10000000
'Gets uploaded fields Set Fields = GetUpload()
'There are all of form fields in the Fields object. Example : 'Fields("File1").ContentType - content type of File1 field 'Fields("File1").Value - Binary value of File1 field ResultHTML = "" If Err = 0 Then 'Upload was OK 'Write statistics about upload dim Field For Each Field In Fields.Items ResultHTML = ResultHTML & " Field : " & LogF(Field.name) & ", Length : " & LogFn(Field.Length) & ", Content-Type : " & LogF(Field.ContentType) & ", SourceFileName :?b>" & LogF(Field.FileName) & "" Next
'Saves the fields to the disk, writes result to the client and writes log. 'See utils.inc. You can change the function to save the files to another location. ResultHTML = ResultHTML & " " & SaveUpload(Fields, Server.MapPath("."), LogFolder) Else 'Error in upload. Write the error ResultHTML = ResultHTML & " Error : " & Err.Description End If On Error GoTo 0 Fields = Empty 'Clear the variable '************************************************* Main Upload - end ' EndTimer 'Writes info about consumed time. End If 'Request method must be "POST"
%>
<%'upload.inc, contains GetUpload function, Required for upload - only the one file%>
<%'utils.inc, contains SaveUpload function%>
<%'format.inc, contains head and Foot function, optional.%>
<%=Head("Sample multiple binary files upload via ASP", "Demonstrates using of the ByteArray class for working with binary data from Request.BinaryRead.")%>
?%=ResultHTML%>
<%=Foot%>
文件fdbutl.asp将文件内容保存如数据库中 <%'upload.inc, contains GetUpload function, Required for upload - only the one file%>
<%'format.inc, contains head and Foot function, optional.%>
<%=Head("Sample database upload via ASP", "Demonstrates using of the ByteArray class for working with binary data from Request.BinaryRead.")%>
<% Function DownloadTime(intFileSize, strModemType) Dim TimeInSeconds, ModemSpeed, strDownloadTime, AppendString Dim intYears, intWeeks, intDays Dim intHours, intMinutes, intSeconds intYears = 0 intWeeks = 0 intDays = 0 intHours = 0 intMinutes = 0 intSeconds = 0 strDownloadTime = "" Select Case strModemType Case "Cable" ModemSpeed = 400000 Case "56kbps" ModemSpeed = 7000 Case "33.6kbps" ModemSpeed = 4200 Case "28.8kbps" ModemSpeed = 3600 End Select TimeInSeconds = int(intFileSize / ModemSpeed) 'year maths added 1/4 of a day. 1 exact orbit of the sub is 365.25 days. If (Int(TimeInSeconds / 31471200) <> 0) Then intYears = Int(TimeInSeconds / 31449600) If ((Int(TimeInSeconds / 604800) Mod 52) <> 0) Then intWeeks = Int(TimeInSeconds / 604800) Mod 52 If ((Int(TimeInSeconds / 86400) Mod 7) <> 0) Then intDays = Int(TimeInSeconds / 86400) Mod 7 If TimeInSeconds >= 3600 Then intHours = Int(TimeInSeconds / 3600) Mod 24 If TimeInSeconds >= 60 Then intMinutes = Int(TimeInSeconds / 60) Mod 60 If TimeInSeconds >= 0 Then intSeconds = Int(TimeInSeconds) Mod 60 If intYears <> 0 Then If intYears = 1 Then AppendString = "" Else AppendString = "s" strDownloadTime = strDownloadTime & intYears & " year" & AppendString & ", " End If If intWeeks <> 0 Then If intWeeks = 1 Then AppendString = "" Else AppendString = "s" strDownloadTime = strDownloadTime & intWeeks & " week" & AppendString & ", " End If If intDays <> 0 Then If intDays = 1 Then AppendString = "" Else AppendString = "s" strDownloadTime = strDownloadTime & intDays & " day" & AppendString & ", " End If If intHours <> 0 Then If intHours = 1 Then AppendString = "" Else AppendString = "s" strDownloadTime = strDownloadTime & intHours & " hour" & AppendString & ", " End If If intMinutes <> 0 Then If intMinutes = 1 Then AppendString = "" Else AppendString = "s" strDownloadTime = strDownloadTime & intMinutes & " minute" & AppendString End If If ((intYears = 0) And (intWeeks = 0) And (intDays = 0) And (intHours = 0)) Then If intSeconds = 1 Then AppendString = "" Else AppendString = "s" If intMinutes > 0 Then strDownloadTime = strDownloadTime & ", " & intSeconds & " second" & AppendString Else strDownloadTime = strDownloadTime & intSeconds & " second" & AppendString End If End If DownloadTime = strDownloadTime End Function %>
It is going to take about <%=DownloadTime(123456,Cable)%> to download this file.