To upload files from within FreeMarker templates, use the standard HTML form "file"
type attribute.
Firstly ensure the following:
controller.xml
request-map to designate the backend Event or Service to call when the form
is submitted.Files can be uploaded in forms by following these steps:
form
. Be sure to include the form
attribute enctype
as"multipart/form-data"
."file"
as shown here:<form action="<@ofbizUrl>myFileUpload</@ofbizUrl>" name="addContentForm" method="post" enctype="multipart/form-data"> <#-- Add the rest of the Form's elements --> <input type="file" name="uploadedFile" size="25" id="uploadedFile" /> </form>
FreeMarker supports all the standard HTML form
input types, including the file
type. Remember, to upload a file you will need an OFBiz Event or Service to accept that file, on the backend.
In support of the OFBiz Content Management Application, there are a number of file upload Services available to use within your webapp. While a detailed discussion of how the Content Manager works is beyond the scope of this book, you can use any of these existing OFBiz Services to help you learn not only how file uploading may be accomplished on the backend, but also how the OFBiz Content Manager Application works.
For example, using the createContentFromUploadedFile
Service within your own webapp allows for a file upload from a browser to the local server hard drive without regards to the file type. This Service will copy any type of file (plain-text, PDF, MSWord, any image format, and others) using binary data transfers to the local OFBiz install sub-directory location ~/runtime/uploads/.
Without any further work by you, beyond:
controller.xml
request-map entry to direct requests to the createContentFromUploadedFile
Service. form
that includes, at a minimum, a contentId
and a dataResourceId
as shown here:<form action="<@ofbizUrl>createContentFromUploadedFile</@ofbizUrl>" name="aForm" method="post" enctype="multipart/form-data"> <input type="hidden" name="dataResourceId" value="DR_01" /> <input type="hidden" name="contentId" value="CT_01" /> <label>Upload File</label> <input type="file" name="uploadedFile" size="30"> <input type="submit" class="button" value="submit" /> </form>
You may easily handle a file upload within your OFBiz web application.
It is left as an exercise to the reader to learn what the dataResourceId
and contentId
values are and what they are used for.
3.128.200.71