请教高手:将本地文件(如C:\test.doc)通过http方式上传到服务器如(http://www.xxx.com/upload/),该怎么做
我采用inet控件
可是运行起来服务器收不到文件,或者文件打不开(打开为乱码),上传
纯英文txt文档倒是可以,有中文又收不到了以下是主要代码,请高手看问题在哪Inet1.URL = "http://www.xxx.com/upload/"
Inet1.Protocol = 4 - icHTTP
Inet1.RemoteHost = "www.xxx.com"
Inet1.RemotePort = 8081
Dim right1 As Boolean
Dim strSendHeader As String
Dim iSendLength As Long
Dim strSendData As String
Dim strFile As String
iSendLength = FileLen(Text1.Text) 'text1放本地文件地址
MsgBox iSendLength
strFile = GetFileContents(Text1.Text)
strSendData = "-----------------------------7d525c2f30476" & vbCrLf
strSendData = strSendData & "Content-Disposition: form-data; name=""file1""; filename=""" & Text1.Text & """" & vbCrLf
strSendData = strSendData & "Content-Type:application/msword" & vbCrLf
MsgBox strSendData
strSendData = strSendData & vbCrLf & vbCrLf & strFile & vbCrLf & "-----------------------------7d525c2f30476--"
strSendHeader = "Referer: http://www.xxx.com/upload/upload.html" & vbCrLf
'html页面是一文件上传页面 strSendHeader = strSendHeader & "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*" & vbCrLf
strSendHeader = strSendHeader & "Accept-charset: UTF-8" & vbCrLf
strSendHeader = strSendHeader & "Accept-Language: zh-cn" & vbCrLf
strSendHeader = strSendHeader & "Content-Type: multipart/form-data; boundary=---------------------------7d525c2f30476" & vbCrLf
strSendHeader = strSendHeader & "Accept-Encoding: gzip , deflate" & vbCrLf
strSendHeader = strSendHeader & "Connection: Keep-Alive" & vbCrLf
strSendHeader = strSendHeader & "Content-Length: " & iSendLength & vbCrLf
Inet1.Execute "http://www.xxx.com/upload/upload.html", "POST", strSendData, strSendHeader
right1 = Inet1.StillExecuting Do While right1
right1 = Inet1.StillExecuting
DoEvents
Loop
Private Function GetFileContents(ByVal strPath As String) As String
Dim StrReturn As String
Dim ValReturn() As Byte
'Dim lngLength As Long
Dim I As Integer
'lngLength = FileLen(strPath)
'StrReturn = String(lngLength, Chr(0))
On Error GoTo ERR_HANDLER
Open strPath For Binary As #1
ReDim ValReturn(LOF(1))
For I = 1 To LOF(1)
Get #1, I, ValReturn(I)
'ValReturn = Input(I, #1)
'MsgBox ValReturn(I)
'MsgBox Chr(ValReturn(I))
StrReturn = StrReturn + Chr(ValReturn(I))
Next
Close #1
GetFileContents = StrReturn
MsgBox GetFileContents
Exit Function
ERR_HANDLER: MsgBox Err.Description, vbCritical, "ERROR"
Err.Clear
End Function
我采用inet控件
可是运行起来服务器收不到文件,或者文件打不开(打开为乱码),上传
纯英文txt文档倒是可以,有中文又收不到了以下是主要代码,请高手看问题在哪Inet1.URL = "http://www.xxx.com/upload/"
Inet1.Protocol = 4 - icHTTP
Inet1.RemoteHost = "www.xxx.com"
Inet1.RemotePort = 8081
Dim right1 As Boolean
Dim strSendHeader As String
Dim iSendLength As Long
Dim strSendData As String
Dim strFile As String
iSendLength = FileLen(Text1.Text) 'text1放本地文件地址
MsgBox iSendLength
strFile = GetFileContents(Text1.Text)
strSendData = "-----------------------------7d525c2f30476" & vbCrLf
strSendData = strSendData & "Content-Disposition: form-data; name=""file1""; filename=""" & Text1.Text & """" & vbCrLf
strSendData = strSendData & "Content-Type:application/msword" & vbCrLf
MsgBox strSendData
strSendData = strSendData & vbCrLf & vbCrLf & strFile & vbCrLf & "-----------------------------7d525c2f30476--"
strSendHeader = "Referer: http://www.xxx.com/upload/upload.html" & vbCrLf
'html页面是一文件上传页面 strSendHeader = strSendHeader & "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*" & vbCrLf
strSendHeader = strSendHeader & "Accept-charset: UTF-8" & vbCrLf
strSendHeader = strSendHeader & "Accept-Language: zh-cn" & vbCrLf
strSendHeader = strSendHeader & "Content-Type: multipart/form-data; boundary=---------------------------7d525c2f30476" & vbCrLf
strSendHeader = strSendHeader & "Accept-Encoding: gzip , deflate" & vbCrLf
strSendHeader = strSendHeader & "Connection: Keep-Alive" & vbCrLf
strSendHeader = strSendHeader & "Content-Length: " & iSendLength & vbCrLf
Inet1.Execute "http://www.xxx.com/upload/upload.html", "POST", strSendData, strSendHeader
right1 = Inet1.StillExecuting Do While right1
right1 = Inet1.StillExecuting
DoEvents
Loop
Private Function GetFileContents(ByVal strPath As String) As String
Dim StrReturn As String
Dim ValReturn() As Byte
'Dim lngLength As Long
Dim I As Integer
'lngLength = FileLen(strPath)
'StrReturn = String(lngLength, Chr(0))
On Error GoTo ERR_HANDLER
Open strPath For Binary As #1
ReDim ValReturn(LOF(1))
For I = 1 To LOF(1)
Get #1, I, ValReturn(I)
'ValReturn = Input(I, #1)
'MsgBox ValReturn(I)
'MsgBox Chr(ValReturn(I))
StrReturn = StrReturn + Chr(ValReturn(I))
Next
Close #1
GetFileContents = StrReturn
MsgBox GetFileContents
Exit Function
ERR_HANDLER: MsgBox Err.Description, vbCritical, "ERROR"
Err.Clear
End Function
非文本数据要用byte模式发送