Link to home
Start Free TrialLog in
Avatar of greatseats
greatseatsFlag for United States of America

asked on

ASP - server to server file transfer - multipart/form-data encoding RFC 1867

Hello,

I am working with a webservice that accepts file uploads.  I am stuck using classic ASP to consume this service and am having a heck of a time figuring out how to send files.

The webservice requests that files be sent using multipart/form-data encoding per RFC 1867.  I have not been able to figure out how to do this with classic ASP.  I am very well versed in posting data/headers using XMLHTTP via classic ASP.  I simply can't figure out how to send the file.

Any help will be greatly appreciated!
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of greatseats

ASKER

Padas,

Thanks for info but I think I've already got that covered.  Here is my current code:

myURL = "http://mywebservice.com"
myToken = "123456"
postData = "fname=john&lname=doe&file="

Set o = server.CreateObject("MSXML2.XMLHTTP")
o.open "POST", myURL, false
o.setRequestHeader "Content-Type", "multipart/form-data"
o.setRequestHeader "Authorization", myToken
o.send postData

My question is: how do I attach my file?  What do I need to do to pull it from my server and push it through with this request as the "file" parameter?

Thanks!
> how do I attach my file?

All you are doing in this piece of code is posting fname=john and lname=doe and file=nothing.

If you are talking about physically uploading a file to another server, you need to look at the docs for that webservice.  Nothing in your code here looks like it will upload a file.
>>postData = "fname=john&lname=doe&file="
That is formatting for enctype="application/x-www-form-urlencoded".

Since you want to "upload" a file, you need to format the data using "form-data" formatting.  This is shown on Section 6 of:
http://www.ietf.org/rfc/rfc1867.txt

Basically you need set your variable postData to something similar to:
Content-Type: multipart/form-data, boundary=<BOUNDARY_TOKEN><CRLF>
<CRLF>
--<BOUNDARY_TOKEN><CRLF>
Content-Disposition: form-data; name="fname"<CRLF>
<CRLF>
john<CRLF>
--<BOUNDARY_TOKEN><CRLF>
Content-Disposition: form-data; name="lname"<CRLF>
<CRLF>
doe<CRLF>
--<BOUNDARY_TOKEN><CRLF>
Content-Disposition: form-data; name="file"; filename="file1.gif"<CRLF>
Content-Type: image/gif<CRLF>
Content-Transfer-Encoding: binary<CRLF>
<CRLF>
<FILE_CONTENT><CRLF>
--<BOUNDARY_TOKEN>--

Open in new window


where <CRLF> is vbCrLF if you are using VBScript.
<BOUNDARY_TOKEN> is some random alphanumeric string/identifier of your choosing.
Lastly, where you see <FILE_CONTENT>, you will need to replace it with the content of the file you want to upload.

You can find functions to read text files or binary files here:
http://www.motobit.com/tips/detpg_read-write-binary-files/