How to POST an image file?

Andre Garzia soapdog at mac.com
Mon May 7 12:11:43 EDT 2007


Joel,

I didn't tried assembling mime encoded messages by hand, but why  
don't you try some not well known libURL gems as detailed on libURL  
manual http://support.runrev.com/resources/liburldocs.php Below, I  
quote from the docs, I think you can use this functions to assemble  
your post data...

libUrlMultipartFormData(<form data>,...)

libUrlMultipartFormData formats data in the way described in rfc  
1867. It was designed for posting files to web servers. The function  
can be called in a number of ways depending on the parameters passed.  
In all cases, the first parameter must be a variable which will be  
filled with the form data. The function will return empty if  
successful, or an error message if it fails (for example, if it  
couldn't open a file).

Note: When you need to supply a file as the value of a paramter, you  
must pass the file name preceeded by the text "<file>".

Note: In all cases, the first line of the data returned in the form  
data variable is the Content-Type header that must be included in the  
httpHeaders of the url request. Lines 2 to the end of the data is the  
data that must be posted to the url.

The standard way to call the function is with pairs of name/value  
parameters.

Example
         put empty into tForm
         put "http://www.someserver.com/cgi-bin/form.cgi" into tUrl
         put "dave" into tName
         put "hello" into tMessage
         put "<file>" & "C:/myfile.txt" into tFile
         if libUrlMultipartFormData (tForm, "name", tName, "message",  
tMessage, "file", tFile) is not empty then
           answer it ##error
         else
           set the httpHeaders to line 1 of tForm
           post line 2 to -1 of tForm to url tUrl
           ## check the result, etc., here
           set the httpHeaders to empty
         end if

You can also pass in an array instead of pairs of parameters. This  
could be useful if there are many parts to a form. The array must be  
numerically indexed, and each element should contain the part name  
and part value, separated by a comma. So (using the above example):

         put empty into tForm
         put "dave" into tName
         put "hello" into tMessage
         put "<file>" & "C:/myfile.txt" into tFile
         put "name," & tName into tArray[1]
         put "message," & tMessage into tArray[2]
         put "file," & tFile into tArray[3]
         if libUrlMultipartFormData(tForm, tArray) is not empty then
          answer it ##error
         else
           set the httpHeaders to line 1 of tForm
           post line 2 to -1 of tForm to url <whatever>

           ## check the result, etc., here
           set the httpHeaders to empty
         end if

You can also call the function with no arguments except the form  
data. This will return an "empty" form. Basically, line 1 is the  
header, and line 2 is the final boundary mark of the form. It is of  
no use by itself, but it can be used with libUrlMultipartFormAddPart.  
See below for an example.



libUrlMultipartFormAddPart(<form data>,<part name>,<value>[,<MIME  
type>,<encoding>])

This lets you add parts to a multipart form one at a time. It also  
lets you optionally specify the mime type and transfer encoding for  
each part. This can be useful where the mime type or transfer  
encoding has to be specified.

Example
         put empty into tForm
         put "dave" into tName
         put "hello" into tMessage
         if libUrlMultipartFormData (tForm, "name", tName, "message",  
tMessage) is not empty then
          ##handle error and exit
         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
         if libUrlMultipartFormAddPart(tForm,"file", tFile, tType,  
tEnc) <> empty then
           ##handle error and exit
         else
           post  tForm to url <whatever>

           set the httpHeaders to empty
         end if

On May 7, 2007, at 1:01 PM, Joel Guillod wrote:

> Given the following form in an html page from url like "http:/ 
> myserver/mydir/mypage":
>
> <form action="myaction" method="post"
> enctype="multipart/form-data">
> <p>File: <input type="file" name="file"></p>
> <input type="submit">
> </form>
>
> I try to "POST tData to URL "http:/myserver/mydir/mypage/myaction"
> where tData contains the following data:
>
> --> (this line is not included in data)
> Content-type: multipart/form-data, boundary=AaB03x
>
> --AaB03x
> content-disposition: form-data; name="file"; filename="logo.gif"
> Content-type: image/gif
> Content-Transfer-Encoding: binary
>
> GIF89a DATA HERE...
> --AaB03x--
>
> <-- (this line is not included in data)
>
> Can anyone tell me why I get a 500 server error or a 40x error?  
> Using the original html page in a web browser works as expected and  
> I have checked that it is not a cookie issue.
>
> I also tried without success (same error) with the following tData:
>
> --> (this line is not included in data)
> Content-type: multipart/form-data, boundary=AaB03x
>
> --AaB03x
> content-disposition: form-data; name="file"
> Content-type: multipart/mixed, boundary=BbC04y
>
> --BbC04y
> Content-disposition: attachment; filename="decoration.gif"
> Content-type: image/gif
> Content-Transfer-Encoding: binary
>
> GIF89a DATA HERE...
> --BbC04y--
> --AaB03x--
>
> <-- (this line is not included in data)
>
> So I suppose either my post url or the data content is/are  
> malformed... Any help welcome!
>
> Thanks,
> Joel
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your  
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution




More information about the use-livecode mailing list