This example illustrates how to extract file upload information from a request. The project consists of only a server component which generates HTML that can be viewed in your browser.
We are transitioning to using JIRA for all bugs and support related issues, therefore the GitHub issues has been disabled.
If you find a mistake, bug, or any other helpful suggestion you'd like to make on the docs please head over to http://jira.perfect.org:8080/servicedesk/customer/portal/1 and raise it.
A comprehensive list of open issues can be found at http://jira.perfect.org:8080/projects/ISS/issues
To use the example with Swift Package Manager, type swift build
and then run .build/debug/UploadEnumerator
.
To use the example with Xcode, run the Upload Enumerator target. This will launch the Perfect HTTP Server.
Navigate in your web browser to http://localhost:8181/
- The server module consists of two relevent files:
- UploadHandler.swift, within which is the
PerfectServerModuleInit
function, which all Perfect Server modules must implement, and theUploadHandler
class, which implements thePageHandler
protocol. - index.mustache, which contains the template for the HTML based response data.
- UploadHandler.swift, within which is the
- When the Upload Enumerator target is built in Xcode it places the resulting product in a directory called PerfectLibraries. When the Perfect Server is launched, it will look in this directory, based on the current process working directory, and load all the modules it finds calling the
PerfectServerModuleInit
function in each. - When the Upload Enumerator target is built in Xcode it also copies the index.mustache file into the directory named webroot. This permits the file to be treated as the default page for the site.
PerfectServerModuleInit
adds a route for the root of the web server, associating with it a closure which will be called to handle each request.- When a request comes in targetting the root of the server, the server will parse the index.mustache file.
- The server calls the
UploadHandler.valuesForResponse
function, which is part of theMustachePageHandler
protocol, passing to it the request'sMustacheEvaluationContext
andMustacheEvaluationOutputCollector
objects which contain all the information pertaining to the request. The return value of thevaluesForResponse
function is a Dictionary object populated with the keys and values used when processing the mustache template. The result of the template processing is the resulting data which will be sent back to the client. - The handler accesses the
WebRequest.fileUploads
property. This is an array containing oneMimeReader.BodySpec
object for each file which was uploaded. TheseBodySpec
objects contain the following:- field name
- file name
- file size
- file content type (as determined by the browser which submitted the file)
- local temporary file name
- the
File
object containing the locally written file data
- The handler iterates over each uploaded file and stores the related information into a dictionary. These dictionary values will be utilized in the mustache template which constitutes the resulting HTML data.
- The handler then iterates over all non-file related parameters which were submitted by the browser. The names and values for each parameter are placed in their own dictionaries. In the resulting template, both file and non-file related submissions will be shown.
- Finally, the handler adds a dictionary item for the page "title" and returns the resulting dictionary from its
valuesForResponse
function. - The index.mustache template first iterates over the supplied
{{#files}}
elements, outputting each piece of uploaded file related data into a table. It then iterates over the non-file{{#params}}
and prints them in a list. The resulting completed HTML data is then returned to the browser.
For more information on the Perfect project, please visit perfect.org.