One more question about Rev cgi & binary data

Dave Cragg dave.cragg at lacscentre.co.uk
Sat Apr 26 19:10:00 EDT 2008


On 25 Apr 2008, at 21:18, jbv wrote:

> Hi guys,
>
> Has anyone already tried to parse data from a multipart/form-data html
> form
> (that contains a type=file element) with Rev cgi ?
> I've done it successfully in the past with ascii data, but now I'm
> trying to post
> jpeg data along with other parameters (height & with of the pic,  
> session
> #...)
> in the same form, and obviously the jpeg binary data get corrupted in
> the process...

I've done this before with jpeg files uploaded as binary data. I can't  
find the script I used, but I think the handler below is what I used  
to parse the form data. It's based on a libCGI script that Jacqueline  
Gay used to distribute. It will handle standard and multipart forms.

If you need to find out how the data was encoded (e.g. whether base64  
or binary) then I think you'll need to look for a "Content-transfer- 
encoding:" header somewhere below the boundary for the file part.

Cheers
Dave

##########################################

//assumes sPostdata already contains the entire posted data
// watch for wrapping in the email

local sCGIData, sPostdata

on parsePostData
	if sPostdata is empty then exit parsePostData
	if matchText($CONTENT_TYPE,"multipart/form-data;  
*boundary=(.*)",tMimeBoundary) then
     	stripQuotes tMimeBoundary
     	
     	put lineOffset(tMimeBoundary,sPostdata) into tStartLine
     	if tStartLine = 0 then exit parsePostdata
     	repeat forever
     		put lineOffset(tMimeBoundary,sPostdata, tStartLine + 1) +  
(tStartLine + 1) into tEndline
     		if tEndline < 1 then exit repeat
			if matchText(line tStartLine to tEndLine of sPostdata,"Content- 
Disposition: +form-data; +name="&quote&"([^"&quote&"]+)",tName) then
				put lineOffset(CRLF & CRLF, line tStartLine to tEndLine of  
sPostdata) + tStartLine + 1 into tDataOffset
				
				put line tDataOffset to tEndLine - 1 of sPostdata into tValue
				rStripCR tValue
				put tValue into sCGIData[tName]
				put tEndLine into tStartLine
			else
				exit repeat
         	end if
     	end repeat
     else  ##assume www form urlencoded
     	if sPostdata <> empty then split sPostdata  by "&" and "="
     	repeat for each line tKey in the keys of sPostData
      	 	put URLDecode(sPostData[tKey]) into sCGIData[tKey]
     	end repeat
    	end if
end parsePostData


More information about the use-livecode mailing list