PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/webupload.arc

http://github.com/alimoeeny/arc
Unknown | 44 lines | 43 code | 1 blank | 0 comment | 0 complexity | 78bc3e0bafcc68ca984358c4793dc874 MD5 | raw file
 1;
 2; Example of uploading a file from a web browser with
 3; the Anarki version of Arc.
 4;
 5; This example just echos back what the browser sends
 6; so you can see how to parse it.  Parsing may depend
 7; on the format of the data that is uploaded.
 8; You'll probably need to parse the boundary out of
 9; the Content-Type and then look for it.  Here's some
10; sample output:
11;
12; ... headers ... 
13; Content-Type: multipart/form-data; boundary=----WebKitFormBoundarysVQ7sAKHmOlv
14; Content-Length: 200
15; ... more headers ...
16;
17; ------WebKitFormBoundarysVQ7sAKHmOlv 
18; Content-Disposition: form-data; name="name"; filename="test.txt" 
19; Content-Type: text/plain 
20; 
21; A small
22; 3 line
23; test file.
24; 
25; ------WebKitFormBoundarysVQ7sAKHmOlv-- 
26;
27; Note that the final boundary an extra "--" at the beginning and
28; at the end.
29(def upload-action (req)
30  (prn " ... headers ...")
31  (prn "<br>Content-Type: " req!ctype)
32  (prn "<br>Content-Length: " req!clen)
33  (prn "<br> ... more headers ...")
34  (prn "<br><br>")
35  (let n req!clen
36    (whilet c (and (> n 0) (readc req!in))
37      (-- n)
38      (if (is c #\newline) (pr "<br>") (pr c))))
39)
40
41(defop upload req
42  (aform-multi upload-action ; must use aform-multi to get the multipart encoding
43    (gentag input type 'file name 'name)
44    (submit "upload")))