[REBOL] finally! (multipart/form-data)
From: ryan::christiansen::intellisol::com at: 30-Apr-2001 17:59
I finally created a routine which will parse multipart/form-data. I ran
into trouble trying to figure out how to keep the image portion of the POST
data binary, but I got it to work in the end.
Go to this web page to see it in action...
http://www.fargonews.com/post-data.html
Here is the code...
#!rebol -cs
REBOL []
print "Content-Type: text/html^/" ;-- Required Page Header
content-length: 20 + load system/options/cgi/content-length
post-data: make string! content-length
read-io system/ports/input post-data content-length
post-data: make binary! post-data
cd-block: []
parse/all post-data [
any [to "Content-Disposition:" copy text to "------" (text: make
binary! text append cd-block text)]
]
cd-input: []
foreach cd cd-block [
either find cd "filename" [
parse/all cd [thru {name="} copy text to {"} (append cd-input
text)]
parse/all cd [thru {filename="} copy text to {"} (file-path-string:
copy text)]
file-path-block: parse/all file-path-string {\/"}
filename: last file-path-block
parse/all cd [thru {Content-Type: } copy text to {^/}
(file-mime-type: copy text)]
parse/all cd [thru {^M^/^M^/} copy text to {^M^/} (text: make
binary! text append cd-input text)]
][
parse/all cd [thru {name="} copy text to {"} (append cd-input text)]
parse/all cd [thru {^M^/^M^/} copy text to {^M^/} (append cd-input
text)]
]
]
cd-input: head cd-input
object-data: make object! []
for x 1 (length? cd-input) 2 [
variable: first cd-input
cd-input: next cd-input
value: first cd-input
either binary? value [
make-object-data: reform [rejoin [{object-data: make object-data [}
(variable) {: } (value) {]}]]
][
make-object-data: reform [rejoin [{object-data: make object-data [}
(variable) {: ^{} (value) {^}]}]]
]
do make-object-data
cd-input: next cd-input
]
make-object-data: reform [rejoin [{object-data: make object-data [filename:
^{} (filename) {^}]}]]
do make-object-data
make-object-data: reform [rejoin [{object-data: make object-data
[file-mime-type: ^{} (file-mime-type) {^}]}]]
do make-object-data
image-name: make file! (rejoin [{../images/} object-data/filename])
write/binary image-name object-data/file
wait 4
print rejoin [{<IMG SRC="} image-name {">}]
Ryan C. Christiansen
Web Developer
Intellisol International
4733 Amber Valley Parkway
Fargo, ND 58104
701-235-3390 ext. 6671
FAX: 701-235-9940
http://www.intellisol.com
Global Leader in People Performance Software
_____________________________________
Confidentiality Notice
This message may contain privileged and confidential information. If you
think, for any reason, that this message may have been addressed to you in
error, you must not disseminate, copy or take any action in reliance on it,
and we would ask you to notify us immediately by return email to
[ryan--christiansen--intellisol--com]