|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
续上,我按照您的代码稍作修改,修改后的代码- Sub Main()
- Const SJR As String = "小明," '论坛UID
- Const GZ As String = "无"
- Dim Boundary As String
- Dim SendData
- Dim FileFullName As String
- Dim FileShortName As String
- Dim Title As String
- Dim Filetype As String
-
- FileFullName = "D:\2014年试考核.xls"
- FileShortName = Mid(FileFullName, InStrRev(FileFullName, "") + 1)
- Title = Left(FileShortName, InStrRev(FileShortName, ".") - 1)
- Filetype = "xls"
-
- '获取Boundary
- Boundary = GetBoundary()
- '获取上传所需的SendData
- SendData = GetUpLoadSendData(Boundary, FileFullName, _
- "sjr", SJR, _
- "gz", GZ, _
- "file1", FileShortName, _
- "B1", "发送邮件", _
- "c1", "否")
-
- '上传
- With CreateObject("WinHttp.WinHttpRequest.5.1")
- .Open "POST", "http://222.68.172.80/data/email1/processsend.asp", False
- .setRequestHeader "Content-Type", "multipart/form-data; boundary=" & Boundary
- .setRequestHeader "Referer", "http://222.68.172.80/data/email1/send.asp"
- .Send SendData
- Debug.Print .responsetext '出现一串数字则为成功。到论坛发帖的界面可看到“未使用的附件”的提示。
- End With
- End Sub
- Function GetBoundary() As String
- '生成Boundary
- Dim i As Integer, r As Integer
- Do While i < 34
- r = Int(Rnd * 75 + 48)
- If r < 58 Or (r > 64 And r < 91) Or r > 96 Then
- GetBoundary = GetBoundary & Chr(r)
- i = i + 1
- End If
- Loop
- GetBoundary = String(4, "-") & GetBoundary
- End Function
- Function GetUpLoadSendData(Boundary As String, FileFullName As String, ParamArray NameValue()) As Byte()
- 'NameValue()必须成双,前一个是名称,后一个是值
- 'NameValue()最后一对是文件流之后的名称值对
- 'NameValue()倒数第二对是文件流信息相关的两个数据
-
- Dim DataBefore, DataAfter
- Dim arrBytData(1 To 3), bytData() As Byte
- Dim i As Long, j As Long, n As Long
-
- '连接文件流之前的各项名称值对
- For i = 0 To UBound(NameValue) - 6 Step 2 '最后三对单独处理
- DataBefore = DataBefore & "--" & Boundary & vbCrLf
- DataBefore = DataBefore & "Content-Disposition: form-data; name=""" & NameValue(i) & """" & vbCrLf
- DataBefore = DataBefore & vbCrLf
- DataBefore = DataBefore & NameValue(i + 1) & vbCrLf
- Next
-
- '连接文件流此项的Content-Disposition
- DataBefore = DataBefore & "--" & Boundary & vbCrLf
- DataBefore = DataBefore & "Content-Disposition: form-data; name=""" & NameValue(i) & """; filename=""" & NameValue(i + 1) & """" & vbCrLf
- DataBefore = DataBefore & "Content-Type: application/vnd.ms-excel" & vbCrLf
- DataBefore = DataBefore & vbCrLf
-
- '文件流前面的字符串转为流
- arrBytData(1) = StrToUTF8Byte(DataBefore)
-
- '文件转流
- arrBytData(2) = FileToByte(FileFullName)
-
- '文件流之后的字符串(两项)
- For i = UBound(NameValue) - 3 To UBound(NameValue) Step 2
- DataAfter = "--" & Boundary & vbCrLf
- DataAfter = DataAfter & "Content-Disposition: form-data; name=""" & NameValue(i) & """" & vbCrLf
- DataAfter = DataAfter & vbCrLf
- DataAfter = DataAfter & NameValue(i + 1) & vbCrLf
- 'DataAfter = DataAfter & "--" & Boundary ' & "--"
- Next i
- DataAfter = DataAfter & "--" & Boundary & "--"
- arrBytData(3) = StrToUTF8Byte(DataAfter) '转为流
-
- '合并字符流和文件流
- ReDim bytData(UBound(arrBytData(1)) + UBound(arrBytData(2)) + UBound(arrBytData(3)) + 2)
- For i = 1 To 3
- For j = 0 To UBound(arrBytData(i))
- bytData(n) = arrBytData(i)(j)
- n = n + 1
- Next
- Next
-
- GetUpLoadSendData = bytData
- End Function
- Function StrToUTF8Byte(strText)
- '文本转UTF-8编码并去除BOM头
- With CreateObject("adodb.stream")
- .Mode = 3 'adModeReadWrite
- .Type = 2 'adTypeText
- .Charset = "GB2312"
- .Open
- .Writetext strText
- .Position = 0
- .Type = 1 'adTypeBinary
- ' .Position = 3 '去除UTF-8编码文本前面的BOM头(三个字节)
- StrToUTF8Byte = .Read()
- .Close
- End With
- End Function
- Function FileToByte(strFileName As String)
- '文件转流
- With CreateObject("Adodb.Stream")
- .Open
- .Type = 1 'adTypeBinary
- .LoadFromFile strFileName
- FileToByte = .Read
- .Close
- End With
- End Function
复制代码 |
|