Uploading files in FreeMarker forms

To upload files from within FreeMarker templates, use the standard HTML form "file" type attribute.

Getting ready

Firstly ensure the following:

  1. Make sure you have a backend OFBiz Event or Service configured to accept the uploaded file.
  2. Configure the controller.xml request-map to designate the backend Event or Service to call when the form is submitted.

How to do it...

Files can be uploaded in forms by following these steps:

  1. Within a new or existing FreeMarker file, create or modify an HTML form. Be sure to include the form attribute enctype as"multipart/form-data".
  2. Add an input type of"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>
    

How it works...

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.

There 's more...

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:

  • Creating a controller.xml request-map entry to direct requests to the createContentFromUploadedFile Service.
  • Creating an HTML 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.200.71