tlconsulting
asked on
Problem posting multipart/form-data to server
I am working on a project written in VB6 where I need to upload a PDF to a server over HTTP. The file must be posted as multipart/form-data. For some reason, when the file gets transmitted, it is getting corrupted. I have verified that the binary data is correct immediately before I post to the server. After I post, some of the binary data is different and the PDF will not display properly. I am using ServerXMLHTTP to post the file. I have included my code, the original PDF (PDF-Before.pdf), the form data that I am posting (PostData-Before.txt), the form data that gets received by the server (PostData-After.txt), and the resulting PDF file (PDF-After.pdf). If I compare the before and after files using WinDiff, there are a few sections that are different in the binary data. It is always the same parts that are different. I would greatly appreciate your assistance with this.
PDF-After.pdf
PostData-Before.txt
PostData-After.txt
Dim objHTTP As ServerXMLHTTP40
Set objHTTP = New MSXML2.ServerXMLHTTP40
Call objHTTP.Open("POST", strURL, False)
Call objHTTP.setRequestHeader("Content-Type", "multipart/form-data; boundary=---------------------------7d83cf5800be")
Call objHTTP.Send(strBody)
PDF-Before.pdfPDF-After.pdf
PostData-Before.txt
PostData-After.txt
How are you opening the pdf file? As a string??
ASKER
The code I am using to retrieve the binary data from the PDF is below. I verified that I can write the PDF back to a file from the string, as shown in the commented out code, and the PDF is not corrupt at that point. It only gets corrupt after is is posted over HTTP.
Function GetBinaryFile(strFileName As String) As String
Dim strFile As String
Dim nFile
' Grap the file
nFile = FreeFile
Open strFileName For Binary Access Read As #nFile
strFile = String(LOF(nFile), " ")
Get #nFile, , strFile
Close #nFile
GetBinaryFile = strFile
'nFile = FreeFile
'Open "c:\temp\stream.pdf" For Binary Access Write As #nFile
'Put #nFile, , strFile
'Close #nFile
End Function
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The post data must be formatted as shown in PostData-Before.txt. I can't just post the raw binary data. I have a separate function that does the formatting as shown below. Since the rest of the data is string data, how would I incorporate the byte array?
bound = "---------------------------7d83cf5800be"
boundSeparator = "--" & bound & vbCrLf
boundFooter = "--" & bound & "--" & vbCrLf
strBody = boundSeparator
strBody = strBody & "Content-Disposition: form-data; name=""" & "ID" & """" & vbCrLf & vbCrLf & gstrFNCUserNameXML
strBody = strBody & vbCrLf & boundSeparator
strBody = strBody & "Content-Disposition: form-data; name=""" & "PASSWORD" & """" & vbCrLf & vbCrLf & gstrFNCPasswordXML
strBody = strBody & vbCrLf & boundSeparator
strBody = strBody & "Content-Disposition: form-data; name=""" & "PORT_ID" & """" & vbCrLf & vbCrLf & strECNClientId
strBody = strBody & vbCrLf & boundSeparator
strBody = strBody & "Content-Disposition: form-data; name=""" & "FOLDER" & """" & vbCrLf & vbCrLf & strTransactionId
strBody = strBody & vbCrLf & boundSeparator
strBody = strBody & "Content-Disposition: form-data; name=""" & "INVOICE_NUM" & """" & vbCrLf & vbCrLf & lngOrderId
strBody = strBody & vbCrLf & boundSeparator
strBody = strBody & "Content-Disposition: form-data; name=""" & "TOTAL_FEE" & """" & vbCrLf & vbCrLf & curFee
strBody = strBody & vbCrLf & boundSeparator
strFileContent = GetBinaryFile(strSavePath & "\" & strFileName)
strBody = strBody & "Content-Disposition: form-data; name=""" & "FILE_REPORT" & """; filename=""" & strSavePath & "\" & strFileName & """" & vbCrLf & _
"Content-Type: application/pdf" & vbCrLf & vbCrLf & strFileContent & vbCrLf
strBody = strBody & boundFooter
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The send method can only be called once on the object. It throws an error if it is called more than once.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.