Skip to main content

File Upload in Websphere Commerce


Websphere Commerce has a inbuilt mechanism to handle the file upload process but it has some limitations with it.
  1. Whenever a File type is present in request object, commerce ignores the rest of parameters coming after that.
  2. The inbuilt mechanism treats the file object as UploadedFile type object. This type of object provides a limited number of options to further process the File Objects.
To  overcome these limitation, we need to use Apache Common Fileupload features to handle the File Upload process for Hanes.

File Upload Basic Requirements



In order to pass a File object in request, we need to make some changes in the Form.


File Upload is not supported by REST or Ajax REST Requests.Icon

Request Parsing



The first step is to parse the request for Uploaded File Objects. And apart from this we need to get the other parameters back to request properties, which is not available because of the limitation of commerce.
To achieve this, we need to have HTTPRequest object. We can get it as below:



Configuring Request Parser

To parse the File Objects, Apache provides ServletFileUpload class. Before it can be used, it needs to be initialized with a DiskFileItemFactory object. And we want to configure this factory before handing over to ServletFileUpload class.
Icon
DiskFileItemFactory has some internal limitation of file size available in request. If the size exceeds, it stores the File on Disk temporarily for processing. By default this is System Temp directory.

So we need to provide the Temporary location where it can save the items. And apart from that we need to set the maximum File size limit.

















If the File object in request exceeds the max size, a FileUploadException is thrown.

Building the Request Properties

Once we have set everything, we need to get the FileItem from request and rebuild the request properties as below:

Save Image



Once we have re-built the request properties, at this time, it contains both normal form data and File objects as well. We can proceed for saving the file.
Before we actually save the file we need to set the file permission as below:




Ajax like File Upload



As said above, FileUpload can not be achieved in pure Ajax way as it needs original HTTP request object for request processing, dojo.io.iframe provides an alternate to handle this in Ajax-Like way. To achieve this, we need to replace the traditional form submit with dojo.io.iframe.send().



To handle the dojo.io.iframe.send() success and error scenarios, we need to use X_MultiPartAjaxActionResponse and X_MultiPartAjaxActionErrorResponse views respectively.


Related links



Using Apache File Upload: Apache File Upload
Disk File Item: DiskFileItemFactory
Servlet File Upload: ServletFileUpload
Multipart Form: W3 Multipart
Ajax like File upload using dojo.io.iframe utilitydojo wiki, dojo documentation


Comments

Popular posts from this blog

Websphere Commerce Creating a custom SOLR core

This page describes how we can create custom SOLR cores in commerce. Let's think of a e commerce site where we retailers can log in and can register there shops. We want to index Shops so that user can search for different shops. SOLR Core Naming Convention A custom SOLR core has to be named according to OOTB convention  MC_10001_<coreName>_en_US . The core name should be a single word without any underscores in it. An entry in solr.xml will look like as below < core   instanceDir = "MC_10001\en_US\Shops\"   name = "MC_10001_Shops_en_US"   /> Registering custom core with Search server Create a new folder along with the other SOLR cores,  "Shops " at /search/solr/home/MC_10001/en_US/ Shops  should have a " conf " folder for configuration XMLs . Define SQL in /conf/wc-data-config.xml and map the database fields with SOLR fields. Define SOLR fields in schema.xml We need to register our custom SO...

Auto Suggestion in Websphere Commerce Search

There are two types of auto suggestions..  1) Suggest keywords based on Search term. 2) Suggest actual Entity based on Search term. The Auto-Suggestion feature is now handled via Search Rest calls in FEP8. Technically it is Term Search for Spellcheck fields. So the whole thing can be summarized as below mentioned steps: /conf/solrconfig.xml Configure request handler for  /terms  URL Configure  wc_textSuggest  and  wc_spellCheck  configuration /conf/schema.xml Define SOLR fields that are too be shown as Auto Suggested keywords when searched for Terms Define Spell check fields which are to be spell corrected for showing suggestions SOLR Configuration Changes   As auto suggested keywords are nothing but different Terms that are being Indexed at the time of commit, we need to define a handler in solrconfig.xml. <!--          WebSphere Commerce terms request han...