-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCFileUpload.cls
More file actions
294 lines (237 loc) · 8.92 KB
/
CFileUpload.cls
File metadata and controls
294 lines (237 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "CFileUpload"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'//////////////////////////////////////////////////////////////////////////////
'@@summary
'@@require
'@@reference
'@@license
'@@author
'@@create
'@@modify
'//////////////////////////////////////////////////////////////////////////////
'//////////////////////////////////////////////////////////////////////////////
'//
'// 公有声明
'//
'//////////////////////////////////////////////////////////////////////////////
'------------------------------------------------------------------------------
' 接口继承
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
' 公有常量
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
' 公有数据类型
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
' 公有变量
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
' 公有API
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
' 事件声明
'------------------------------------------------------------------------------
'//////////////////////////////////////////////////////////////////////////////
'//
'// 私有声明
'//
'//////////////////////////////////////////////////////////////////////////////
'------------------------------------------------------------------------------
' 私有常量
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
' 私有数据类型
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
' 私有变量
'------------------------------------------------------------------------------
Private mWinHttpReq As WinHttp.WinHttpRequest '声明一个对象
Private mList As CList
'------------------------------------------------------------------------------
' 属性变量
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
' 私有API
'------------------------------------------------------------------------------
'//////////////////////////////////////////////////////////////////////////////
'//
'// 类
'//
'//////////////////////////////////////////////////////////////////////////////
'------------------------------------------------------------------------------
' 初始化
'------------------------------------------------------------------------------
Private Sub Class_Initialize()
Set mWinHttpReq = New WinHttp.WinHttpRequest
Set mList = New CList
End Sub
'------------------------------------------------------------------------------
' 销毁
'------------------------------------------------------------------------------
Private Sub Class_Terminate()
Set mWinHttpReq = Nothing
Set mList = Nothing
End Sub
'//////////////////////////////////////////////////////////////////////////////
'//
'// 事件处理
'//
'//////////////////////////////////////////////////////////////////////////////
'//////////////////////////////////////////////////////////////////////////////
'//
'// 私有属性
'//
'//////////////////////////////////////////////////////////////////////////////
'//////////////////////////////////////////////////////////////////////////////
'//
'// 私有方法
'//
'//////////////////////////////////////////////////////////////////////////////
Private Function LoadData(ByVal FilePath As String) As Byte()
'加载文件到字节数组
Dim Bin() As Byte, FileLength As Long
Open FilePath For Binary As #1 '以二进制方式打开文件
FileLength = LOF(1) ''获取文件长度’该大小以字节为单位
ReDim Bin(FileLength - 1) '用于为动态数组变量重新分配存储空间。
Get #1, , Bin '将一个已打开的磁盘文件读入一个变量之中。
Close #1
LoadData = Bin
End Function
Private Function mGetFileName(ByVal mFilePath As String) As String
'从文件路径中获取文件名
mFilePath = Replace(mFilePath, "\\", "\")
mGetFileName = Mid(mFilePath, InStrRev(mFilePath, "\") + 1)
End Function
Private Function BinString(ByVal content As String) As Byte()
'获取字符串的字节数组
Dim source() As Byte, i As Long, countUp As Long, dest() As Byte
source = content
countUp = 0
If Len(content) = 0 Then
Exit Function
End If
ReDim dest(UBound(source))
For i = 0 To UBound(source)
If source(i) <> 0 Then
dest(countUp) = source(i)
countUp = countUp + 1
End If
Next i
ReDim Preserve dest(countUp - 1)
BinString = dest
End Function
Private Function mGetHost(ByVal mUrl As String) As String
'http://127.0.0.2:80/
Dim beginPos As Long, endPos As Long
beginPos = InStr(1, mUrl, "//")
If beginPos + 2 > Len(mUrl) Then
mGetHost = ""
Exit Function
Else
beginPos = beginPos + 2
End If
endPos = InStr(beginPos, mUrl, "/")
If endPos = 0 Then endPos = Len(mUrl) + 1
mGetHost = Mid(mUrl, beginPos, endPos - beginPos)
End Function
Private Sub mJoinBin(dest() As Byte, source() As Byte)
'合成字节数组
Dim baseSize As Long, i As Long
If UBound(dest) > 0 Then
baseSize = UBound(dest) + 1
ReDim Preserve dest(UBound(dest) + UBound(source) + 1)
Else
baseSize = 0
ReDim dest(UBound(source))
End If
For i = 0 To UBound(source)
dest(baseSize + i) = source(i)
Next i
End Sub
Private Function BytesToBstr(strBody As Variant, ByVal CodeBase As String)
'编码转换("UTF-8"或者"GB2312"或者"GBK")
Dim ObjStream
Set ObjStream = CreateObject("Adodb.Stream")
With ObjStream
.Type = 1
.Mode = 3
.Open
.Write strBody
.position = 0
.Type = 2
.charset = CodeBase
BytesToBstr = .ReadText
.Close
End With
Set ObjStream = Nothing
End Function
'//////////////////////////////////////////////////////////////////////////////
'//
'// 继承实现
'//
'//////////////////////////////////////////////////////////////////////////////
'//////////////////////////////////////////////////////////////////////////////
'//
'// 公有属性
'//
'//////////////////////////////////////////////////////////////////////////////
'//////////////////////////////////////////////////////////////////////////////
'//
'// 公有方法
'//
'//////////////////////////////////////////////////////////////////////////////
Public Function UploadFile(ByVal URL As String, ByVal FilePath As String, Optional ByVal filename As String = "") As String
On Error GoTo lastsave
'获取验证码图片的文字
Dim boundary As String
Dim part1 As String, part3 As String
Dim data() As Byte, bindata() As Byte
boundary = "------------------------------831b7f6f6a2d"
ReDim bindata(0)
data = LoadData(FilePath)
If filename = "" Then
filename = mGetFileName(FilePath)
End If
With mList
.Clear
.Add boundary & vbCrLf
.Add "Content-Disposition: form-data; name=""upfile""; filename=""" & filename & """" & vbCrLf
.Add "Content-Type: application/octet-stream" & vbCrLf
.Add "" & vbCrLf
part1 = .Join("")
.Clear
.Add "" & vbCrLf
.Add boundary & "--" & vbCrLf
part3 = .Join("")
End With
mJoinBin bindata, BinString(part1)
mJoinBin bindata, data
mJoinBin bindata, BinString(part3)
mWinHttpReq.Open "POST", URL, True
mWinHttpReq.SetTimeouts 30000, 30000, 30000, 30000
mWinHttpReq.SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 5.1; zh-CN; rv:1.9.1.3) Gecko/20100101 Firefox/8.0"
mWinHttpReq.SetRequestHeader "Host", mGetHost(URL)
mWinHttpReq.SetRequestHeader "Accept", "*/*"
mWinHttpReq.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & Mid(boundary, 3)
mWinHttpReq.Send bindata '发送
mWinHttpReq.WaitForResponse '异步发送
UploadFile = BytesToBstr(mWinHttpReq.ResponseBody, "UTF-8")
Exit Function
lastsave:
Err.Clear
Debug.Print Err.description
UploadFile = "network-error"
End Function