One of the first problems I ran into when I started building RESTful webservices was figuring out how to handle image uploads and downloads. It turned out it was harder to find good information than to actually do it, so I decided to build an example application and share it with others.
Update: the more recent Reminders example includes this functionality in a full application.
The source code is available on GitHub.
BASE_DIRto point to an existing directory on your server (or localhost) where you want to store the images.
MAX_SIZE_IN_MBto the maximum allowed size you want to use. You will not be able to upload or download images larger than this size.
.pngand not larger than the maximum allowed size) into the
BASE_DIRfolder you configured or use
upload.htmlto upload some images (see below).
The webservice uses the following URLs:
GET this URL to get a list of available images (filenames wrapped in a JSON array). POST to this URL to upload an image. The server will store this image and pick a random filename for it.
GET this URL to download the image with the given filename.
If you change any of these URLs, make sure you update the
BASE_URL variables in
download.js as well.
The front-end consists of the following pages:
Use this page to upload an image via an HTML form.
Use this page to get a list of available images and to download them.
If you've enjoyed my work or found it helpful, please consider becoming a patron. Your support helps me free up time to work on my books and projects.