Uploading a File with HTTP (Post)
Author: Carl Sassenrath Return to REBOL Cookbook
Here is simple example of how to upload a file using HTTP.
This code has been tested and proven to work on our server.
(And REBOL/IOS is based on this code, so we know it works
quite well.)
The Uploader
The code below is the upload script.
REBOL [Title: "HTTP Post Uploader"]
url: http://www.rebol.net/cgi-bin/test/post.r
data: read/binary %/c/rebol/rebol.exe
print ["Sending Size:" length? data "Checksum:" checksum data]
result: read/custom url reduce ['post data]
print ["Server Replied:" result]
|
It will read a file (as binary) and post it to the web server
using HTTP POST. Note the REDUCE in front of the POST block.
Do not forget it.
The Server Script
Here is the script you need on your web server. Put this in your
CGI-BIN directory and make sure that it has execute permissions
(also change the first line to be the correct path to REBOL):
#!/home/web/rebol -cs
REBOL [Title: "HTTP Post Tester"]
print ["content-type: text/plain" newline]
read-post: func [/local data buffer][
either system/options/cgi/request-method = "POST" [
data: make string! 1020
buffer: make string! 16380
while [positive? read-io system/ports/input buffer 16380][
append data buffer
clear buffer
]
][
data: system/options/cgi/query-string
]
data
]
data: read-post
print ["Size:" length? data "checksum:" checksum data]
|
This script will read the data and return a string that
indicates the size and checksum for the data received by the
server.
Of course, you can expand on this script with code that saves
the data to disk or generates an HTML web page result, etc.
Want to try it?
The CGI script above is actually installed on our REBOL web
server, so you can try it out. Run the first script, and you
will get a result something like this:
do %upload.r
Sending Size: 268288 Checksum: 11356574
Server Replied: Size: 268288 checksum: 11356574
|
The size and checksum values should match.
Version and CGI Warning |
Make sure that you use a recent version of REBOL. Earlier
versions had a bug that created a length mismatch on the server.
Also, when you use CGI scripts, there are many things that can
go wrong. See Fixing Your
CGI Script for a complete list of what can go wrong and how
to fix them.
|
|