[plt-scheme] CGI file upload

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Thu Aug 6 12:43:58 EDT 2009

On Wed, Aug 5, 2009 at 6:09 PM, Emmanuel
Delaborde<emmanuel.delaborde at cimex.com> wrote:
> Hello
> I am turning a small web application using web/server into a cgi script
> so far so good, but I got stuck when I tried rewriting my file upload
>  function
> the form to upload an image was:
> (form ((action ,(make-url add-image))
>                            (method "post")
>                            (enctype "multipart/form-data"))
>                           (input ((type "file")(name "image")))
>                           (input ((type "submit"))))
>
> and the form handler was:
> (define (add-image request)
>             (match
>                 (bindings-assq #"image" (request-bindings/raw request))
>               [(? binding:file? b)
>                (let ([filename (bytes->string/utf-8 (binding:file-filename
> b))])
>                  (save-image! (binding:file-content b) filename images-path)
>                  (portfolio-insert-image! a-portfolio filename))
>                ])
>             (admin (redirect/get)))
> this worked fine.
> I got the filename with: (binding:file-filename b)
> and the file content with: (binding:file-content b)
> Now I am trying to write the same function using what is provided by
> net/cgi, namely :
> (get-bindings)
> (extract-bindings key? bindings)
> (extract-binding/single key? bindings)
> It seems pretty straightforward when it comes to simple form values, but not
> so with a file upload...
> Can I get both the file name and the file content through the bindings
> functions ?
> Has anybody a simple cgi script dealing with file upload, laying around ?
> Thanks
> E.

You have discovered the reason to get rid of the extract-bindings API.

I don't know of any work around for CGI scripts. Perhaps the file
content is just on stdin after get-bindings... but I really don't
know.

You could try piping stdin to the web-server http request parser and
use it's http structs, but from a cgi script, but I haven't tried it
before.

Jay

-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://teammccarthy.org/jay

"The glory of God is Intelligence" - D&C 93


Posted on the users mailing list.