fukamachi / caveman

Lightweight web application framework for Common Lisp.
http://8arrow.org/caveman/
782 stars 63 forks source link

multipart file upload #63

Closed knobo closed 9 years ago

knobo commented 9 years ago

Hi,

How does one upload files to caveman2 I have this simple form, but I'm not able to get the contets.

if caveman2 can not do this, can I inject a hunchentoot handler that does that part?

    <form action="/upload" method="post"  enctype="multipart/form-data">
        <input type="file" name="fileToUpload" id="fileToUpload"/>
      <input id="submit" type="submit" value="Submit">
    </form>
knobo commented 9 years ago

I see. https://github.com/fukamachi/caveman/issues/6

The version I downloaded from quicklisp did not include include that fix. (I'm nut sure what the directory name means. clack-20150302-git ) I will update my distribution.

blasut commented 8 years ago

Hi, how do you actually use download and save the file to your local server?

knobo commented 8 years ago

You could try something like this:


(defparameter *file-storage-directory* #p"/var/www/images/")

(defroute ("/upload-image" :method :post) (&key |file|)
  (let ((filename (gethash "filename" (second |file|)))
    (data (slot-value (first |file|) 'flexi-streams::vector)))
    (with-open-file (s (merge-pathnames filename *file-storage-directory*) 
               :if-does-not-exist :create 
               :if-exists :supersede  :direction :output
               :element-type  '(unsigned-byte 8))
      (write-sequence data s)))
  (return-some-result-to-browser))

I'm planning to do something like this in the future (I have not tested it yet):

(setf *image-storage*
      (make-instance 'disk-storage
             :bucket "file-upload-example"
             :directory #P"/tmp/attachment/"))

(defroute ("/upload-image" :method :post) (&key |file|)
  (let ((filename (gethash "filename" (second |file|)))
    (data (first |file|))
    (mito-attachment:*storage* *image-storage*))
    (mito:create-dao 'image :content data)
    (return-some-result-to-browser)))