Does anyone have an example for this, because I this must have been done
before. I'm not too good in C/C++ so a VB example would be nice.
Thanx, Kimmo
' Name : HttpPostFromFile
' Purpose : Send a POST request contains a specified file
' Author : Giampaolo Cimino (gci...@virgilio.it)
' Date : 26-07-2000
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function HttpPostFromFile(ByVal documento As String, _
ByVal FileNameToSend As String, _
ByRef ReturnString As String, _
ByRef result As HTTPTransactionResult, _
ByVal User As String, _
ByVal Password As String) As Boolean
On Local Error GoTo error_handler
Dim hInternetOpen As Long
Dim hInternetConnect As Long
Dim hHttpOpenRequest As Long
Dim bRet As Boolean
Dim lret As Long
Dim iret As Integer
Dim filenumb As Integer
Dim macchina As String
Dim doc As String
Dim BufferIn As INTERNET_BUFFERS
Dim abBin() As Byte
Dim sbinfile As String
Dim nbinfile As Integer
Dim dwpostsize As Long
Dim n As Long
Dim letti As Long
Dim pbuffer As String
Dim MyOffset As Long
Dim chunks As Long
Dim BytesRemain As Long
Dim ChunkLen As Long
Dim bDoLoop As Boolean
Dim sReadBuffer As String * 2048
Dim lNumberOfBytesRead As Long
Dim sHeader As String
'Definitions
ChunkLen = 2048
On Error GoTo error_handler
hInternetOpen = 0
hInternetConnect = 0
hHttpOpenRequest = 0
result.strRETURN_ERROR = vbNullString
result.intRETURN_ERROR = 0
macchina = URLToMachineName(documento)
doc = URLToPathFileName(documento)
hInternetOpen = InternetOpen("GeopacME HTTP Ext", _
INTERNET_OPEN_TYPE_PRECONFIG, _
vbNullString, _
vbNullString, _
0)
If hInternetOpen = 0 Then
result.intRETURN_ERROR = Err.LastDllError
result.strRETURN_ERROR =
TranslateErrorCode(CLng(result.intRETURN_ERROR))
HttpPostFromFile = False
Else 'internetopen ok
hInternetConnect = InternetConnect(hInternetOpen, _
macchina, _
INTERNET_DEFAULT_HTTP_PORT, _
User, _
Password, _
INTERNET_SERVICE_HTTP, _
0, _
0)
If hInternetConnect = 0 Then
result.intRETURN_ERROR = Err.LastDllError
result.strRETURN_ERROR =
TranslateErrorCode(CLng(result.intRETURN_ERROR))
HttpPostFromFile = False
Else 'internetConnect Ok
hHttpOpenRequest = HttpOpenRequest(hInternetConnect, _
"POST", _
doc, _
"HTTP/1.0", _
vbNullString, _
0, _
INTERNET_FLAG_NO_CACHE_WRITE, _
0)
If hHttpOpenRequest = 0 Then
result.intRETURN_ERROR = Err.LastDllError
result.strRETURN_ERROR =
TranslateErrorCode(CLng(result.intRETURN_ERROR))
HttpPostFromFile = False
Else 'HTTPopenrequest OK
sHeader = "Content-Type: application/x-octet-stream" & vbCrLf
iret = HttpAddRequestHeaders(hHttpOpenRequest, _
sHeader, _
Len(sHeader), _
HTTP_ADDREQ_FLAG_REPLACE Or _
HTTP_ADDREQ_FLAG_ADD)
If iret = 0 Then
result.intRETURN_ERROR = Err.LastDllError
result.strRETURN_ERROR =
TranslateErrorCode(CLng(result.intRETURN_ERROR))
HttpPostFromFile = False
Else 'HttpAddRequestHeaders ok
sbinfile = FileNameToSend
nbinfile = FreeFile
Open sbinfile For Binary Access Read Lock Write As #nbinfile
ReDim abBin(ChunkLen)
dwpostsize = LOF(nbinfile)
BufferIn.dwStructSize = 40 ' Must be set or error will
occur
BufferIn.Next = 0
BufferIn.lpcszHeader = 0
BufferIn.dwHeadersLength = 0
BufferIn.dwHeadersTotal = 0
BufferIn.lpvBuffer = 0
BufferIn.dwBufferLength = 0
BufferIn.dwBufferTotal = dwpostsize 'This is the only member
used other than dwStructSize
BufferIn.dwOffsetLow = 0
BufferIn.dwOffsetHigh = 0
'HSR_INITIATE try with the second last param.
bRet = HttpSendRequestEx(hHttpOpenRequest, _
BufferIn, _
0, _
0, _
0)
If (bRet = False) Then
result.intRETURN_ERROR = Err.LastDllError
result.strRETURN_ERROR =
TranslateErrorCode(CLng(result.intRETURN_ERROR))
HttpPostFromFile = False
Else 'SendRequestEx ok
chunks = dwpostsize \ ChunkLen
BytesRemain = dwpostsize - (chunks * ChunkLen)
MyOffset = 1
n = 0
bRet = True
Dim RealChunkLen As Long
RealChunkLen = 0
While (n < chunks) And (bRet = True)
Get #nbinfile, MyOffset, abBin
pbuffer = StrConv(abBin, vbUnicode)
bRet = InternetWriteFile(hHttpOpenRequest, _
pbuffer, _
ChunkLen, _
letti)
n = n + 1
Debug.Print "send " & n
MyOffset = 1 + (n * ChunkLen)
Wend
If bRet = False Then
result.intRETURN_ERROR = Err.LastDllError
result.strRETURN_ERROR =
TranslateErrorCode(CLng(result.intRETURN_ERROR))
HttpPostFromFile = False
Else 'all internetwritefile (- the last) ok
ReDim abBin(BytesRemain)
Get #nbinfile, MyOffset, abBin
pbuffer = StrConv(abBin, vbUnicode)
'pbuffer = pbuffer & "--AaBbCcDd00--"
bRet = InternetWriteFile(hHttpOpenRequest, _
pbuffer, _
BytesRemain, _
letti)
Close #nbinfile
If bRet = False Then
result.intRETURN_ERROR = Err.LastDllError
result.strRETURN_ERROR =
TranslateErrorCode(CLng(result.intRETURN_ERROR))
HttpPostFromFile = False
Else 'the last internetwritefile ok
Dim param As Long
param = 0
'HSR_INITIATE penultimo
lret = HttpEndRequest(hHttpOpenRequest, 0, 0, 0)
If lret = 0 Then
result.intRETURN_ERROR = Err.LastDllError
result.strRETURN_ERROR =
TranslateErrorCode(CLng(result.intRETURN_ERROR))
HttpPostFromFile = False
Else 'HttpEndRequest ok
result.HTTP_QUERY_CONTENT_TYPE =
getQueryOption(HTTP_QUERY_CONTENT_TYPE, hHttpOpenRequest)
result.HTTP_QUERY_CONTENT_LENGTH =
getQueryOption(HTTP_QUERY_CONTENT_LENGTH, hHttpOpenRequest)
result.HTTP_QUERY_LAST_MODIFIED =
getQueryOption(HTTP_QUERY_LAST_MODIFIED, hHttpOpenRequest)
result.HTTP_QUERY_PRAGMA =
getQueryOption(HTTP_QUERY_PRAGMA, hHttpOpenRequest)
result.HTTP_QUERY_EXPIRES =
getQueryOption(HTTP_QUERY_EXPIRES, hHttpOpenRequest)
result.HTTP_QUERY_VERSION =
getQueryOption(HTTP_QUERY_VERSION, hHttpOpenRequest)
result.HTTP_QUERY_STATUS_CODE =
getQueryOption(HTTP_QUERY_STATUS_CODE, hHttpOpenRequest)
result.HTTP_QUERY_STATUS_TEXT =
getQueryOption(HTTP_QUERY_STATUS_TEXT, hHttpOpenRequest)
result.HTTP_QUERY_RAW_HEADERS =
getQueryOption(HTTP_QUERY_RAW_HEADERS, hHttpOpenRequest)
result.HTTP_QUERY_RAW_HEADERS_CRLF =
getQueryOption(HTTP_QUERY_RAW_HEADERS_CRLF, hHttpOpenRequest)
result.HTTP_QUERY_FORWARDED =
getQueryOption(HTTP_QUERY_FORWARDED, hHttpOpenRequest)
result.HTTP_QUERY_SERVER =
getQueryOption(HTTP_QUERY_SERVER, hHttpOpenRequest)
result.HTTP_QUERY_USER_AGENT =
getQueryOption(HTTP_QUERY_USER_AGENT, hHttpOpenRequest)
result.HTTP_QUERY_SET_COOKIE =
getQueryOption(HTTP_QUERY_SET_COOKIE, hHttpOpenRequest)
result.HTTP_QUERY_REQUEST_METHOD =
getQueryOption(HTTP_QUERY_REQUEST_METHOD, hHttpOpenRequest)
If (Val(result.HTTP_QUERY_STATUS_CODE) <>
200) Then
result.intRETURN_ERROR =
Val(result.HTTP_QUERY_STATUS_CODE)
result.strRETURN_ERROR =
result.HTTP_QUERY_STATUS_TEXT
HttpPostFromFile = False
Else 'Not 200
bDoLoop = True
While bDoLoop = True And bRet = True
sReadBuffer = vbNullString
bRet =
InternetReadFile(hHttpOpenRequest, _
sReadBuffer,
_
Len(sReadBuffer), _
lNumberOfBytesRead)
ReturnString = ReturnString & _
Left(sReadBuffer,
lNumberOfBytesRead)
If Not CBool(lNumberOfBytesRead)
Then bDoLoop = False
Wend
If (bRet = False) Then
result.intRETURN_ERROR =
Err.LastDllError
result.strRETURN_ERROR =
TranslateErrorCode(CLng(result.intRETURN_ERROR))
HttpPostFromFile = False
Else 'InternetReadFile Ok
result.intRETURN_ERROR = 0
result.strRETURN_ERROR =
vbNullString
HttpPostFromFile = True
End If
End If ' Not 200
End If 'HttpEndRequest
End If 'last internetwritefile
End If 'all the InternetWriteFile (- the last)
End If 'SendRequestEx
bRet = InternetCloseHandle(hHttpOpenRequest)
If (bRet = False) Then
result.intRETURN_ERROR = Err.LastDllError
result.strRETURN_ERROR =
TranslateErrorCode(CLng(result.intRETURN_ERROR))
HttpPostFromFile = False
End If
End If 'HttpAddRequestHeaders ok
End If 'HTTPopenrequest
bRet = InternetCloseHandle(hInternetConnect)
If (bRet = False) Then
result.intRETURN_ERROR = Err.LastDllError
result.strRETURN_ERROR =
TranslateErrorCode(CLng(result.intRETURN_ERROR))
HttpPostFromFile = False
End If
End If 'InternetConnect
bRet = InternetCloseHandle(hInternetOpen)
If (bRet = False) Then
result.intRETURN_ERROR = Err.LastDllError
result.strRETURN_ERROR =
TranslateErrorCode(CLng(result.intRETURN_ERROR))
HttpPostFromFile = False
End If
End If 'InternetOpen
Exit Function
error_handler:
result.strRETURN_ERROR = Err.Description
result.intRETURN_ERROR = Err.Number
HttpPostFromFile = False
Exit Function
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Name : TranslateErrorCode
' Purpose : Provides message corresponding to DLL error codes
' Parameters : The DLL error code
' Return val : String containing message
' Algorithim : Selects the appropriate string
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function TranslateErrorCode(ByVal lErrorCode As Long) As String
Select Case lErrorCode
Case 12001: TranslateErrorCode = "No more handles could be generated at this
time"
Case 12002: TranslateErrorCode = "The request has timed out."
Case 12003: TranslateErrorCode = "An extended error was returned from the
server."
Case 12004: TranslateErrorCode = "An internal error has occurred."
Case 12005: TranslateErrorCode = "The URL is invalid."
Case 12006: TranslateErrorCode = "The URL scheme could not be recognized, or
is not supported."
Case 12007: TranslateErrorCode = "The server name could not be resolved."
Case 12008: TranslateErrorCode = "The requested protocol could not be
located."
Case 12009: TranslateErrorCode = "A request to InternetQueryOption or
InternetSetOption specified an invalid option value."
Case 12010: TranslateErrorCode = "The length of an option supplied to
InternetQueryOption or InternetSetOption is incorrect for the type of option
specified."
Case 12011: TranslateErrorCode = "The request option can not be set, only
queried. "
Case 12012: TranslateErrorCode = "The Win32 Internet support is being
shutdown or unloaded."
Case 12013: TranslateErrorCode = "The request to connect and login to an FTP
server could not be completed because the supplied user name is incorrect."
Case 12014: TranslateErrorCode = "The request to connect and login to an FTP
server could not be completed because the supplied password is incorrect. "
Case 12015: TranslateErrorCode = "The request to connect to and login to an
FTP server failed."
Case 12016: TranslateErrorCode = "The requested operation is invalid. "
Case 12017: TranslateErrorCode = "The operation was canceled, usually
because the handle on which the request was operating was closed before the
operation completed."
Case 12018: TranslateErrorCode = "The type of handle supplied is incorrect
for this operation."
Case 12019: TranslateErrorCode = "The requested operation can not be carried
out because the handle supplied is not in the correct state."
Case 12020: TranslateErrorCode = "The request can not be made via a proxy."
Case 12021: TranslateErrorCode = "A required registry value could not be
located. "
Case 12022: TranslateErrorCode = "A required registry value was located but
is an incorrect type or has an invalid value."
Case 12023: TranslateErrorCode = "Direct network access cannot be made at
this time. "
Case 12024: TranslateErrorCode = "An asynchronous request could not be made
because a zero context value was supplied."
Case 12025: TranslateErrorCode = "An asynchronous request could not be made
because a callback function has not been set."
Case 12026: TranslateErrorCode = "The required operation could not be
completed because one or more requests are pending."
Case 12027: TranslateErrorCode = "The format of the request is invalid."
Case 12028: TranslateErrorCode = "The requested item could not be located."
Case 12029: TranslateErrorCode = "The attempt to connect to the server
failed."
Case 12030: TranslateErrorCode = "The connection with the server has been
terminated."
Case 12031: TranslateErrorCode = "The connection with the server has been
reset."
Case 12036: TranslateErrorCode = "The request failed because the handle
already exists."
Case Else: TranslateErrorCode = "Error details not available."
End Select
End Function
Private Function getQueryOption(info As String, hHttpOpenRequest As Long) As
String
Dim sBuffer As String * 1024
Dim lBufferLength As Long
Dim intRes As Integer
sBuffer = vbNullString
lBufferLength = Len(sBuffer)
intRes = HttpQueryInfo(hHttpOpenRequest, info, ByVal sBuffer, lBufferLength,
0)
If intRes > 0 Then
getQueryOption = sBuffer
Else
getQueryOption = vbNullString
End If
End Function
Public Type HTTPTransactionResult
intRETURN_ERROR As Integer
strRETURN_ERROR As String
HTTP_QUERY_CONTENT_TYPE As String
HTTP_QUERY_CONTENT_LENGTH As String
HTTP_QUERY_LAST_MODIFIED As String
HTTP_QUERY_EXPIRES As String
HTTP_QUERY_PRAGMA As String
HTTP_QUERY_VERSION As String
HTTP_QUERY_STATUS_CODE As String
HTTP_QUERY_STATUS_TEXT As String
HTTP_QUERY_RAW_HEADERS As String
HTTP_QUERY_RAW_HEADERS_CRLF As String
HTTP_QUERY_FORWARDED As String
HTTP_QUERY_SERVER As String
HTTP_QUERY_USER_AGENT As String
HTTP_QUERY_SET_COOKIE As String
HTTP_QUERY_REQUEST_METHOD As String
End Type
<FORM NAME="uploadform" enctype="multipart/form-data" method="post"
action="/upload/UploadHandler.asp">
<INPUT type="file" name="filename" size=15>
<INPUT TYPE="SUBMIT" VALUE="Upload">
</FORM>
Does it affect, that you are using 'application/x-octet-stream' in your
upload function? Do I have to make some modifications into the component in
the servers end because of this?
"Giampaolo Cimino" <cim...@saclantc.nato.int> wrote in message
news:3A8D5DFE...@saclantc.nato.int...
-text/plain: ascii data
-text/html: html data
-application/url-uncoded (check www.w3.org for the right value!): for
url-encoded data (that is %20 instead of a SPACE,....).
-application/x-octet-stream: for generic binary data
-image/jpeg: form a jpg image
....
There are a lot of pre-defined content type value: More details in www.w3.org.
When you POST something to a server side application I'm quite sure that the
Content Type value is used only from this server side application
(cgi,asp,cf...).
But some Web server could make a control....I don't know very well...you have to
make some test.
Bye
Giampaolo
Thanx, Kimmo
"Giampaolo Cimino" <cim...@saclantc.nato.int> wrote in message
news:3A94059C...@saclantc.nato.int...