Upload files into a Structr application
The following HTML form can be used to upload files into a Structr application. The uploading user (if logged in) will automatically be the owner of the files.
<form action="/structr/upload" method="post" enctype="multipart/form-data">
<input type="hidden" name="parent" value="${first(find('File', 'name', 'uploads'))}">
<input type="hidden" name="redirectOnSuccess" value="/success-page?newFile=">
<input type="hidden" name="appendUuidOnRedirect" value="true">
<input type="hidden" name="visibleToPublicUsers" value="true">
<input type="file" name="uploadedFile">
<input type="submit" value="Upload!">
</form>
Note the action
attribute of the form. That is the URL of the Structr upload servlet which will handle the request. The method
and enctype
attributes contain the default values necessary for uploading files.
The important part are the <input>
fields inside the form. You can set arbitrary properties, such as the parent
folder (which will be set to the uploads
folder if one exists). The name of the uploaded file will be set automatically.
The new feature for consists of the two parameters redirectOnSuccess
and appendUuidOnRedirect
, which can be used to instruct Structr to redirect the browser to a custom URL when the upload is finished. If you supply appendUuidOnRedirect
with a value of true
, you can for example redirect the user to a new page with a detail view of the page he/she just uploaded.
The above form will redirect the user to the following URL when the upload is finished, provided that the UUID of the uploaded file is de1067b758b24ee08b57021eae8659dd
:
/success-page?newFile=de1067b758b24ee08b57021eae8659dd
Note
The <input type="file" />
field must be the after the input fields for attributes for this file!
This is because the HTML form elements are processed sequentially. This was a deliberate decision as the HTML standard allows multiple file elements in a form and it is undefined for which file element the attributes should be valid. The rule in Structr is that form attributes are valid until they are overwritten by another value. This way you can upload multiple files in one request with all files being of different types (achievable by adding an input file with name=“type” and value=“FileTypeXYZ”) between the different files.