Avatar uploads are working!

Post #: 68
Date: 2012-11-19 20:52:42.000
Author: Rocket Man

After a fair bit of fiddling, I've gotten avatar uploads working on the newLISP on Rockets blog. Now you can upload your own image.

The code to display a file upload form looks like this:

(displayln "<p>Upload new avatar (all avatars scaled to 64x64 pixels): <form name='FileUpload' action='rockets-avatarupload.lsp' method='POST' enctype='multipart/form-data'><input type='file' id='uploadName' name='uploaded_data' onChange='this.form.textname.value = this.value'><input type='hidden' name='textname'><input type='submit' value='Upload' name='submit'></form>")

The form variables are put into the ($POST) tree, along with a variable called "filename" that holds the file name, and "binary-data" that holds the file itself.

To retrieve these from ($POST), just use the following code:

(set 'file-name ($POST "filename"))
(set 'file-binary ($POST "binary-data"))

Then to save the file , I use the following command:

(write-file (string "images/avatars/" file-name) file-binary)

One bit of warning: you will need to set the ownership permissions on the directory where you are saving these files to something that the Apache user has write access to. In Ubuntu this was something like:

sudo chown www-data:www-data images/avatars

And that's it! The avatar images are not scaled when they are uploaded, but they are displayed with fixed dimensions of 64x64 pixels. This means if you upload a larger image it will look tiny. In the future I will add a module that automatically resizes files to a dimension of your choice, but for now, try to keep avatars to no larger than 100x100.

Rocketeer on 2012-11-28 13:57:14.000

Working on post counts and one hella meta post rating system?
Just asking. :)

Rocket Man on 2012-11-28 15:17:40.000

Post counts are coming. Post ratings probably won't be implemented until traffic gets a lot larger.

View this post in the forums

Views: 3180