Page 1 of 1

LibUrlMultipartFormData

Posted: Wed Feb 18, 2009 1:53 am
by keyless
I am trying to write a script that will upload a file to a php file upload form. (it is one field with a file choose butten then a submit button)


Here is the code of the form cut from the source:

Code: Select all

Select an image file to upload<br>
Max file size is set at 793 KB<br><br>
<form method="post" enctype="multipart/form-data" action="basic.php"> 
<INPUT TYPE="file" NAME="userfile" size="27">
<br><br><br>
<input type="submit" value="Upload" name="upload">
</form>
<center><br><br><br>
Now should I be trying to post the URL of the file to the form page? or should I be posting the file to the basic.php page?

Should I be posting at all? I don't see the post command used at all in the doc examples.

very confused.

Posted: Wed Feb 18, 2009 6:41 am
by Janschenkel
This is pretty close to the docs example for libURLMultipartformAddPart - so the following should be close.

Code: Select all

on mouseUp
   put "http://www.example.com/basic.php" into tURL
   put empty into tForm
   put "Upload" into tUpload
   get libUrlMultipartFormData(tForm, "upload", tUpload)
   if it is not empty then
      ##handle error and exit
      answer error "Couldn't setup multipart form data:" & return & it
      exit mouseUp
   end if
   set the httpHeaders to line 1 of tForm
   delete line 1 of tForm
   put "<file>" & "C:/myfile.gif" into tFile
   put "image/gif" into tType
   put "binary" into tEnc
   get libUrlMultipartFormAddPart(tForm,"file", tFile, tType, tEnc)
   if it is not empty then
      ##handle error and exit
      answer error "Couldn't add part to multipart form data:" & return & it
      exit mouseUp
   else
      post  tForm to url tURL
      set the httpHeaders to empty
   end if
end mouseUp
Things to bear in mind:
  • You need to use the post command because that's specified in the original html as the form's 'method' attribute.
  • You also need to include the name-value pair for the 'Upload' submit button.
HTH,

Jan Schenkel.

Posted: Wed Feb 18, 2009 6:42 am
by keyless
Ugg there just isn't enough info on these functions. I even found the LibURL docs but with only one example per function its not much help.

Ok, i've figured out that I want to be posting to the basic.php url (that is the "action" of the form).

Now I have built the post using libUrlMultipartFormData

here is what my post looks like (with only a little of the binary image data showing for obvious reasons) I put it into msg.

Code: Select all

Content-type: multipart/form-data; boundary="__Part__7pudncoedfii310w6zjmggr2g"
--__Part__7pudncoedfii310w6zjmggr2g

Content-Disposition: form-data; name="userfile"; filename="C:\picture.jpg"

Content-Type: application/octet-stream



ÿØÿà
And binary Gibbrish continues....



I also put the result I get from the basic.php into message. It states that the file is not of correct file type, but it is a .jpg, and I can uplaud the same file through the actual form just fine.

Code: Select all

<font color="#333333" face="Geneva, Arial, Helvetica, sans-serif">You selected a wrong filetype!</font><br>
I've pretty much hit a dead end. please I'm sure I'm just missing something like encoding or something.

Posted: Wed Feb 18, 2009 6:45 am
by keyless
Janschenkel wrote:This is pretty close to the docs example for libURLMultipartformAddPart - so the following should be close.

Code: Select all

on mouseUp
   put "http://www.example.com/basic.php" into tURL
   put empty into tForm
   put "Upload" into tUpload
   get libUrlMultipartFormData(tForm, "upload", tUpload)
   if it is not empty then
      ##handle error and exit
      answer error "Couldn't setup multipart form data:" & return & it
      exit mouseUp
   end if
   set the httpHeaders to line 1 of tForm
   delete line 1 of tForm
   put "<file>" & "C:/myfile.gif" into tFile
   put "image/gif" into tType
   put "binary" into tEnc
   get libUrlMultipartFormAddPart(tForm,"file", tFile, tType, tEnc)
   if it is not empty then
      ##handle error and exit
      answer error "Couldn't add part to multipart form data:" & return & it
      exit mouseUp
   else
      post  tForm to url tURL
      set the httpHeaders to empty
   end if
end mouseUp
Things to bear in mind:
  • You need to use the post command because that's specified in the original html as the form's 'method' attribute.
  • You also need to include the name-value pair for the 'Upload' submit button.
HTH,

Jan Schenkel.
Oh I cross posted you Jan. Thanks for the help I'll take a look at your script.

Posted: Wed Feb 18, 2009 7:09 am
by keyless
That did not work at all at first.

Post formed and looks like it sent, but image was not uploaded.

I then put a "put it into msg" right after the POST line. I get empty.


I noticed you had "File" in the liburlmultipartFormAddPart. The field name of the file uplaud field is actually "userfile" so I swiched that.

no post formed an is sent. The result I get back though is:

Code: Select all

<font color="#333333" face="Geneva, Arial, Helvetica, sans-serif">You selected a wrong filetype!</font><br>
Same as I got with my own attempts. Jpg is certainly an accepted file type when I use the actual form.

Here is what the Post looked like in msg (I left out the binary data)

Code: Select all

--__Part__wcbstsmubcty2hyxjnmfaoc3z

Content-Disposition: form-data; name="upload"



Upload

--__Part__wcbstsmubcty2hyxjnmfaoc3z

Content-Disposition: form-data; name="userfile"; filename="picture.jpg"

Content-Type: image/jpg

Content-transfer-encoding: binary



ÿØÿà
still stumped will have to sleep on it.

Posted: Wed Feb 18, 2009 10:45 am
by SparkOut
If you change the type to

Code: Select all

image/jpeg
does that work any better?
The standard mime type declaration for a jpg file (as per http://www.w3schools.com/media/media_mimeref.asp ) is with an "e" in the jpEg reference. If the receiving server is checking to make sure that the right sort of file is being uploaded (which it will be - nobody wants an executable file to be uploaded in place of a jpg which will then get executed and trash the server/clients/the internet) then it will probably be rejecting the declaration of image/jpg.

Posted: Wed Feb 18, 2009 5:32 pm
by keyless
SparkOut wrote:If you change the type to

Code: Select all

image/jpeg
does that work any better?
The standard mime type declaration for a jpg file (as per http://www.w3schools.com/media/media_mimeref.asp ) is with an "e" in the jpEg reference. If the receiving server is checking to make sure that the right sort of file is being uploaded (which it will be - nobody wants an executable file to be uploaded in place of a jpg which will then get executed and trash the server/clients/the internet) then it will probably be rejecting the declaration of image/jpg.

:D SparkOut you just made my day! I changed it to jpeg and it worked perfectly! Thank you so much.

Thank you Jan also for pointing me in the right direction.

Posted: Wed Feb 18, 2009 11:56 pm
by SparkOut
I just did the little bit, Jan deserves the credit for making your day! Glad it works now.

And how do you clear a POST that didn't complete?

Posted: Tue Mar 03, 2009 7:50 pm
by rayjbenet
This question isn't directly on this thread, but I'm guessing the folks on this thread can answer this question:

When I get the error: "Previous Request Not Completed" when I am trying to do a POST, how do I clear the post that failed ?

the code snippet here is:

Code: Select all

 post myFileContents to myURL
   if the result is NOT empty then
      answer "Tasking Push Failed: " & return &  the result
   end if