[plt-scheme] file uploads with PLT web server

From: Greg Pettyjohn (gregp at ccs.neu.edu)
Date: Mon Aug 2 18:00:20 EDT 2004

On Jul 26, 2004, at 2:06 AM, Alexander Boldakov wrote:

>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> Hello,
>
> what is the recommended way to upload files via web forms with PLT web
> server?
> How can a servlet access the transferred file?
>
>
>
> -- 
> Best regards,
>  Alexander                          mailto:boldakov at ispras.ru
>
>

It really isn't all that tough. In summary, just do the following

1. Publish a form that includes an input tag of type file.
2. When you get the request back, pull out the bindings with the 
request-bindings selector.
3. Use extract-binding/single to get the file contents. The file 
contents will be associated with
the name attribute from the original input tag.
4. The result will be a string containing all the file contents.

Here's a short sample compliments of Mike Burns:

(require (lib "unitsig.ss")
          (lib "servlet-sig.ss" "web-server")
          (lib "servlet-helpers.ss" "web-server"))

(unit/sig ()
   (import servlet^)

   ;; get-filename-page : String -> Response
   (define (get-filename-page k-url)
     `(html (head (title "Gimme a file"))
            (body (h1 "Gimme a file")
                  (form ((action ,k-url)
                         ;; Method and enctype are important
                         (method "POST")
                         (enctype "multipart/form-data"))
                        (p (label ((for "file")) "File")
                           (input ((name "file") (id "file") (type 
"file"))))
                        (p (input ((type "submit"))))))))

   ;; get-file : -> String
   (define (get-file)
     (extract-binding/single
       'file
       (request-bindings (send/suspend get-filename-page))))

   (send/finish
     `(html (head ((title "File's done!")))
            (body (h1 "File's done!")
                  (p "The file is uploaded with contents ")
                  (pre ,(let ((port (open-output-string)))
                          (write (get-file) port)
                          (get-output-string port))))))
   )



Posted on the users mailing list.