[REBOL] Re: How to file upload via http?
From: gchiu:compkarori at: 22-Aug-2002 17:09
>What should %savefile.r look like?
I can't recall if I've made any changes to this, but I
recall testing it and I think it worked for me.
#!/path/to/rebol -cs
REBOL [
Title: "multipart POST"
Date: 15-Sep-1999
Version: 1.2
File: %POST.r
Author: "Andrew Grossman (modified by: Luis Rodriguez J
-.29-April-2000)"
Email: [grossdog--dartmouth--edu]
Owner: "REBOL Technologies"
Purpose: {
To decode multipart encoded POST requests,
including file uploads.
}
Usage: {
Call this file in your CGI scriptand do
decode-multi with a
file argument of the directory where uploaded
files will go and
a logic argument to set whether files will be
given the field they
were
uploaded as as a name. Files are saved and
variables are decoded
and
set.
}
Notes: {
Fixed problem recognizing EOF.
Functionality is now rock solid. Function
calls won't change, so this is certainly useable.
See the comment in the decode-multi function if
you need mime types.
Tested with MSIE and Netscape for Mac.
}
category: ['web 'cgi 'utility]
]
print "Content-Type: text/html^/^/"
print {<HTML><BODY>}
decode-multi: func [
file-dir [file!] {Directory in which to put uploaded
files}
save-as-field [logic!] {save files as field name or
uploaded filename}
/local str boundary done name file done2 content
] [
if equal? system/options/cgi/request-method "POST" [
either not parse system/options/cgi/content-type
["multipart/form-data" skip thru {boundary=} skip some {-}
copy boundary to end]
; not multipart
[str: make string! input do decode-cgi str]
[
print "Decoding multipart data"
str: make string! input
done: false
while [not done] [
name: make string! ""
str: make string! input
either equal? "" str
[done: true]
[
either parse/all str [skip thru {name="} copy name to
{"}
skip thru {filename="} copy file to {"} skip to end]
[
str: make string! input
if not equal? str "" [str: make string! input]
comment {if you need mime put "parse/all str
["Content-Type:"
skip copy mime to end]" into the
preceding if
block.}
done2: false
content: make string! ""
while [not done2] [
content-length: to-integer
system/options/cgi/content-length
str: make string! content-length
read-io system/ports/input str content-length
either d: find/reverse tail str boundary [
e: find/reverse tail copy/part str (index? d)
{^/}
content: copy/part str (index? e) - 2
done2: true]
[
append content str
]
]
write/binary %content.bin str
if not none? file [
either save-as-field [name: dehex name write/binary
file-dir/:name content] [
file: dehex file write/binary file-dir/:file
content
]
]
] [
parse str [skip thru {name="} copy name to {"}]
str: make string! input str: dehex make string!
input
set to-word name str str: make string! input
]
]
]
]
]
] ; of If Post
decode-multi %. true
print "Form Processed"
print {</BODY></HTML>}
--
Graham Chiu