tags:

views:

875

answers:

2

Hi,

I'm using Visual Basic 6 and want to do an HTTP post to a server (it runs Java code) by sending a custom input field along with a PDF file. the PDF file would have to be base 64 bit encoded or use the normal way that HTTP post work over the Internet when uploading a file. Basically I just want to upload a file from my Visual Basic 6 program.

How do I do this? Any example source code?

Thanks

A: 

You can use the winsock control for this. Here is a sample vb application for File upload.

Shoban
A: 

Assuming you know how to load the PDF in to a byte array you've got to get it Base64 encoded and then post that to server using mime multipart encoding.

You can utalise the MSXML libraries ability to perform Base64 encoding. See this link for details.

Once you have the PDF as a Bas64 string you need to package that as Mime multipart. You can use XMLHTTP object from MSXML to perform that posting for you:-

sEntityBody = "----boundary" & vbCrLf
sEntityBody = sEntityBody & "Content-Dispostion: form-data; name=fileInputElementName; filename=""" + sFileName + """" & vbCrLf
sEntityBody = sEntityBody & "Content-Transfer-Encoding: base64" & vbCrLf
sEntityBody = sEntityBody & "Content-Type: application/pdf" &  vbCrLf & vbCrLf
sEntityBody = sEntityBody & sPDFBase64 & vbCrLf
sEntityBody = sEntityBody & "-----boundary--" & vbCrLf & vbCrLf

Set xhr = New MSXML2.XMLHTTP30
xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=-----boundary")
xhr.Open "POST", sUrl, False
xhr.send sEntityBody

Perhaps not elegant or efficient but it should it work.

AnthonyWJones
Hi, I'm getting an unexpected error message when using your code above :-(
@Lennie: Where do get "unexpected error" message?
AnthonyWJones
Its working now but the send() command is nagging when I send the sEntityBody to it. it gives me an error that the parameter is incorrect.
@Lennie: The last comment is unclear its working now? What did you have to change?
AnthonyWJones