[REBOL] Re: upload files via http with Rebol?
From: SunandaDH:aol at: 18-Dec-2004 11:49
Eric:
> Does anyone know where I can find out how to allow users to upload files
> via their web browsers and save them with Rebol?
I went round in circles working this out at the beginning of this year.
This is what I ended up with.
STEP 1 -- get read-cgi
You may have this function already -- you'll be using it for non-multipart
files.
If not, it's here:
http://www.rebol.com/docs/words/wread-io.html
STEP 2 -- get code to decode multipart CGI data
Luckily, Andreas did the hard part for us, and published it on the Mailing
List.
Available from:
http://www.rebol.org/cgi-bin/cgiwrap/rebol/ml-display-message.r?m=rmlKVSQ
with some further discussion here:
http://www.rebol.org/cgi-bin/cgiwrap/rebol/ml-display-thread.r?m=rmlZYMQ
STEP 3: The HTML
As with all other CGI languages, you need some HTML.
Something like this for a simple page that has a single file to upload
(Andreas' code works happily with multiple files on the same page).
You'll need to add the name of your CGI program in the action tag
<form enctype="multipart/form-data" action="...your cgi program..."
method="post">
<div>
<input type="text" name="non-mp1" size="20">
<input type="file" name="mp1" size="50">
<input type="submit" alt="update button" name="update" value="update">
</div>
</form>
STEP 4 -- Process it in a CGI program
This code is somewhat convoluted as it covers the case where
the input data does not contain any multi-part fields. (It's written that
way so a single CGI program can both put out a blank form and
process the results from it).
Your code can be simpler if the CGI is used only to read a page with
multi-part data.
post-data: read-cgi
error? try [cgi-object: construct decode-cgi copy post-data]
error? try [cgi-object: construct cons-utils/decode-multipart-form-data
system/options/cgi/content-type
copy post-data
]
if error? try [cgi-object/non-mp1]
[ error? try [cgi-object: construct decode-cgi copy post-data]]
;; You can now do things with the uploaded file, eg
write/binary %saved-file cgi-object/mp1
You can see the end results of this here....
http://www.rebol.org/cgi-bin/cgiwrap/rebol/package-information.r?script-name=l
ds-demo1-package.r
.....the page is listing mainly JPGs that were uploaded this this code.
Sunanda.