Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

POSTing a binary file using multipart/form-data

Status
Not open for further replies.

clarkin

Programmer
Dec 4, 2002
707
IE
Hi all,

I'm trying to setup an ASP page to POST an image across to another page- essentially simulating what a browser does when you use <input type=file> in a HTML form.

I'm able to correctly setup the headers etc and do the POST, but I'm unable to include the binary data of the image. The only way i've been able to do it is if I base64 encode the image. I'm using MSXML2.ServerXMLHTTP to do the POST, and I can't seem to do the .send with a form body that includes the binary data of the image.

So, here's the form body i'm sending:
Code:
---------------------491299511942
Content-Disposition: form-data; name="fileupload"; filename="small.gif"
Content-Type: image/gif
Content-Transfer-Encoding: base64

R0lGODlhCgAKAIAAAP8AAAAAACH5BAAAAAAALAAAAAAKAAoAAAIIhI+py+0PYysAOw==
---------------------491299511942--
And here's what I want to send (this is what a browser sends when you use a form and browse to the same file):
Code:
-----------------------------304891371731000
Content-Disposition: form-data; name="fileupload"; filename="small.gif"
Content-Type: image/gif

GIF89a
 
 €  ÿ     !ù     ,    
 
  „?©Ëíc+ ;
-----------------------------304891371731000

And here's some of the code used:
Code:
DIM xmlhttp
SET xmlhttp = SERVER.CREATEOBJECT("MSXML2.ServerXMLHTTP")
xmlhttp.OPEN "POST", URL, FALSE
xmlhttp.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
xmlhttp.setRequestHeader "Content-Length", len(filecontents)
xmlhttp.send formData
return = xmlhttp.responsetext
SET xmlhttp = NOTHING

I can read the binary data from the image using ADODB.Stream, but I can't seem to make a formData variable that will have that AND the plain text parts of the form. Anyone able to help?

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Google gave me this client-side script for posting a binary file using the xmlhttp object... perhaps you can convert it to your needs.
Code:
function sendFile(filname) {
    var adoStream = new ActiveXObject("ADODB.Stream");
    adoStream.Mode = 3; // read write
    adoStream.Type = 1; // adTypeBinary
    adoStream.Open();
    adoStream.LoadFromFile(filname);
    var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
    filname = filname.substr(filname.lastIndexOf("\\")+1);
    xmlhttp.Open("POST", "BinaryStream.asp?filename=" + filname, false);
    xmlhttp.setRequestHeader("Content-Length", adoStream.Size);
    xmlhttp.send(adoStream.Read(adoStream.Size));
    alert(xmlhttp.responseText);
}
 
Hi Sheco,

That would indeed be simple to convert, however it would just POST to the target page with only the binary file contents as the post body. ie, it wouldn't include the multipart/form-data stuff the target page is expecting.

My problem is being able to do
Code:
xmlhttp.send(<string data of form><binary data of file><rest of string data>)
.read on a ADODB.stream returns a variant, and I can build a string variant for the rest but trying to combine them gives me a type mismatch. Anyone have an idea how to do this?

I will try just firing the binary data at the listening page though :)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Let me see if I understand...

You have some "binary" data in a byte array.

You have some text in a variant.

Attempting to concatinate the byte array onto the variant throws a type-mismatch error.

So you want to convert the text into a byte array of ASCII character values so that it can be concatinated with your byte array of "binary" data?
 
Yep exactly. Then hopefully xmlhttp.send lets me send a byte array as the form body parameter.

Here's how i'm getting the binary stream into a variable:
Code:
Set Stream = CreateObject("ADODB.Stream")
Stream.Type = 1 'Binary
Stream.Open
Stream.LoadFromFile filePath
fileContents = Stream.Read
Stream.Close
Set Stream = nothing

formData = "--" & boundary & vbCrLf & "Content-Disposition: form-data; name=""fileupload""; filename=""test1332.jpg""" & vbCrLf & "Content-Type: image/jpeg" & vbCrLf & "Content-Transfer-Encoding: base64" & vbCrLf & vbCrLf & fileContents & vbCrLf & "--" & boundary & "--"

fileContents correctly gets the binary data of the file, but the & fileContents in the last line breaks. So yeah, if I could create formData as a byte array we might be in business..

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Does it work if you go in reverse? I mean if you MIME encode the "binary" to convert it to ASCII does that work?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top