Chapter 5. File Upload

While being able to submit a form or other post variables via Ajax is a useful thing, sooner or later most applications will want to let a user upload a file, for example an avatar image or a video.

Yaws allows an application to receive a file from the user in the standard upload format that users of other server-side technologies such as PHP have come to expect.

When uploading a file in PHP, PHP buffers the input into the /tmp directory and then sends the finished file to your program. In Yaws, the server sends the file in chunks to your code. So instead of getting a complete file, the programmer has to be ready to get a stream of chunks of data. This does put a little more work onto the programmer, but it also allows the programmer to work with an incomplete upload if that is desired.

The examples in this chapter are taken from the example on the Yaws documentation website with a few minor changes. The full working code is in Example 5-4, while the examples preceding it show parts of the code for explanation.

Note

To test uploading, you can of course use a browser, but using the curl[9] program from a command line makes everything easier to test. Using a command line like this one will upload a file from the local disk to the Yaws server. In this example, the file you specify should be copied to the upload directory, /tmp/YawsUploads—if that directory does not exist it will be created. When the upload is finished, the web server will return an HTML fragment with ...

Get Building Web Applications with Erlang now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.