[racket] Upload progress bar project

From: Eduardo Bellani (ebellani at gmail.com)
Date: Thu Sep 23 00:21:42 EDT 2010

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello list.

I am trying to get a HTML upload progress bar to work with Racket's
web server. Here is some of my design notes. If someone would want to
help me, it would be nice since I am trying to come up with a blog
post to showcase the use of racket's web server (and learn a bit).

* A way to keep polling the server and update a field in the page with
  the %. I am thinking about just saving the address to a function in
  a hidden field. I would use that function to return the % based on
  how much of the file is already written to disk.

* A way to know the full size of the file being uploaded.

* A way to calculate how much of a stream of bytes I have written to
  disk.

* Anything else I don't know yet.

Any tips regarding any of those items would be appreciated.
Here is the basic racket code I have so far:

#lang web-server/insta

; render-upload-page: request -> html-response
; Produces an html-response page of the content of the
; upload page
(define (render-upload-page request)
  (local [(define (response-generator make-url)
            `(html (head (title "Uploader")
                         (script ((src "/uploader.js")) empty))
                   (body
                    (form ((id "upload-form")
                           (action
                            ,(make-url insert-upload-handler))
                           (method "post")
                           (enctype "multipart/form-data")
                           (target "upload_target")
                           (onsubmit "startUpload();"))
                          (input ((name "the-file")
                                  (type "file")))
                          (input ((type "submit")))
                          (br)
                          (label ((name "display-%"))
                                 ,(format "~a %" progress))
                          (input ((type "hidden")
                                  (id "progress-so-far")
                                  (value
                                   ,(make-url progress-so-far))))))))

          (define (insert-upload-handler request)
            (match (bindings-assq #"the-file" (request-bindings/raw
request))
              [(? binding:file? the-file)
               (display (binding:file-filename the-file))])
            (render-upload-page (redirect/get)))]
    (send/suspend/dispatch response-generator)))


;; progress-so-far : request -> number
(define (progress-so-far request)
  0)


; start: request -> html-response
; Consumes a request, and produces a page that displays
; all of the web content.
(define (start request)
  (render-upload-page request))

(static-files-path "./")

(no-web-browser)

- -- 
Eduardo Bellani

omnia mutantur, nihil interit.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkya1dUACgkQSbLl0kCTjGmVwgCfcnCJGzOcqrVPEWj9OL9F3dk0
SRAAn1Oz3mSZzOnpJ+7BEurSAJJ68NHv
=384r
-----END PGP SIGNATURE-----


Posted on the users mailing list.