[REBOL] multipart/form-data
From: jason:cunliffe:verizon at: 30-Aug-2002 8:04
Andreas, you kindly posted a multipart/form-data script for uploading files via
HTTP with some instructions in a later email. Alas I am struggling still to get
HTTP upload working smoothly in REBOL.
Your solution is now __almost__ working for me.
But man I feel so dumb :-(
Some simple basic cgi object thang I am not grokking yet. I hope something can
help me.
I really want to get this cooking with Vanilla :-)
1. I saved the script as %upload-andreas.r [attached]
2. Called by a web form:
<form method="post" action="upload-andreas.r" enctype="multipart/form-data" />
<b>upload-andreas.r</b>
<p>Select an image or script file to upload </p>
<fieldset>
<legend>File to upload:<br /></legend>
<input type="hidden" name="MAX_SIZE" value="100000" /><br/>
Enter filename: <input type="file" name="filename" size="50" /><br/>
<input type="submit" value="upload" background-color: #FFFF00 /><br/>
</fieldset>
</form>
<p>
3. Then as you advised..
;Friday, August 23, 2002, 12:38:09 AM, Jason wrote:
;> Can you help me understand how to use it?
; Andreas => First you fetch the post-data (the HTTP POST entity body, to be
correct) as always
len: load any [ system/options/cgi/content-length "0"]
post-data: make string! (len + 10)
while [len > 0][ len: len - read-io system/ports/input post-data len ]
; Then this POST-data can be handed over to decode-multipart-form-data
; (if it is encoded according to the mime multipart RFC's as
; indicated by a content type beeing multipart/form-data)
tmp-post: either found? find any [ system/options/cgi/content-type "" ]
["multipart/form-data"]
[ decode-multipart-form-data system/options/cgi/content-type post-data ]
[ decode-cgi post-data ]
^^^^
This did not work :-(
** Script Error: tmp-post is missing its p-ct-dispo argument ** Where: do-boot
** Near: tmp-post
Q1: Why?
So I just tried a very minimal version:
A. tmp-post: decode-cgi post-data
ok no compaints.
But when I try this:
B. tmp-post: decode-multipart-form-data system/options/cgi/content-type
post-data
I keep getting the same Script Error as above.
So I stuck with the basic decode-cgi post-data version (A.)
write %test.jpg tmp-post
print rejoin [{<a href="test.jpg">tmp-post</a>}]
4.Select a jpeg file to upload from my form, et voila -> when I click on the
tmp-post link I can see the form headers and content of my Jpeg images as acii
like this:
-----------------------------7d22e4fe68
Content-Disposition: form-data; name"MAX_SIZE"
100000
-----------------------------7d22e4fe68
Content-Disposition: form-data; name="description"
describe me
-----------------------------7d22e4fe68
Content-Disposition: form-data; name="filename";
filename="E:\FLASH\jasonMX\buddha.jpg"
Content-Type: image/pjpeg
JFIFHH File written by Adobe Photoshop 5.2 .........etc etc
etc............
Q2: How to I access the tmp-post object? For example the filename or
description?
Q3: How to save just the image contents as a jpeg file, and link it for display.
Thanks very much for any help to crack this nut
./Jason
-- Attached file included as plaintext by Listar --
-- File: upload-andreas.r
#!/home/rebol/rebol -cs
REBOL [
Title: "decode-multipart-form-data"
Authors: ["Andreas Bolka" [andreas--bolka--gmx--net]]
testing: [ "Jason" [jasonic--nomadics--org] 2002-08-30]
Date: 2002-06-18
Version: 1.1
Purpose: {
Decodes POST-data encoded as "multipart/form-data" as defined by RFC 2388.
The output is compatible to 'decode-cgi wherever possible.
So the output contains a list of set-word's and values, one pair for each data field.
example: [ field1: "foo" field2: "bar" ]
If the content type of one part of the form-data is not equal to
text/plain, then the value of the respective mime-part is an
object! with the following attribs: filename, type, content.
example: [
field1: "foo"
field2: make object! [
filename: "user.xml"
type: "text/xml"
content: ""
]
]
}
]
decode-multipart-form-data: func [
p-content-type
p-post-data
/local list ct pd bd delim-beg delim-end mime-part ] [
list: copy []
if not found? find p-content-type "multipart/form-data" [ return list ]
ct: copy p-content-type
pd: copy p-post-data
bd: join "--" copy find/tail ct "boundary="
delim-beg: join crlf crlf
delim-end: rejoin [ crlf bd ]
mime-part: [
( ct-dispo: content: none ct-type: "text/plain" )
thru bd
thru "content-disposition: " copy ct-dispo to crlf
opt [ thru "content-type: " copy ct-type to crlf ]
thru delim-beg copy content to delim-end
( handle-mime-part ct-dispo ct-type content )
]
handle-mime-part: func [ p-ct-dispo p-ct-type p-content /local fieldname ] [
p-ct-dispo: parse p-ct-dispo [describe ;=" here]
fieldname: select p-ct-dispo "name"
append list to-set-word fieldname
either found? find p-ct-type "text/plain" [append list content][
append list make object! [
filename: select p-ct-dispo "filename"
type: copy p-ct-type
content: either none? p-content [ none ] [ copy p-content ]
]
]
]
use [ ct-dispo ct-type content ] [
parse/all pd [ some mime-part ]
]
return list
]
]
;Friday, August 23, 2002, 12:38:09 AM, Jason wrote:
;> Can you help me understand how to use it?
; Andreas => First you fetch the post-data (the HTTP POST entity body, to be correct)
as always
len: load any [ system/options/cgi/content-length "0"]
post-data: make string! (len + 10)
while [len > 0][ len: len - read-io system/ports/input post-data len ]
; Then this POST-data can be handed over to decode-multipart-form-data
; (if it is encoded according to the mime multipart RFC's as
; indicated by a content type beeing multipart/form-data)
;;** Script Error: tmp-post is missing its p-ct-dispo argument ** Where: do-boot ** Near:
tmp-post
;tmp-post: either found? find any [ system/options/cgi/content-type "" ] ["multipart/form-data"]
;[ decode-multipart-form-data system/options/cgi/content-type post-data ]
;[ decode-cgi post-data ]
tmp-post: decode-cgi post-data
;tmp-post: decode-multipart-form-data system/options/cgi/content-type post-data
;After that you'll have a decode-cgi like structure in tmp-post which
;is described in full detail in the headers of the script i posted before.
print "Content-Type: text/html^/"
;print "Content-Type: image/jpeg^/"
print "hello from %upload-andreas.r"
write %test.jpg tmp-post
print rejoin [{<a href="test.jpg">tmp-post</a>}]