14-Nov-07 (Created: 14-Nov-07) | More in 'Howto-Advanced'

How to upload a file to a database and bring it back

General Idea

1. You will be using commons file upload plugin or part

2. You will need to add a url rewrite filter that comes with aspire to convert extra path to url arguments

3. You will "singlefileuploadpart" to parse the multipart upload

4. You will use a PSExecutor2 part to save it as a blob in the database

5. You can BlobTransform to write it back to the browser

web.config for url filters


<filter>
   <filter-name>DisplayParamFilter</filter-name>
   <display-name>DisplayParamFilter</display-name>
   <description>DisplayParamFilter</description>
   <filter-class>com.ai.servlets.paramfilters.DisplayParamFilter</filter-class>
   <init-param>
      <param-name>parm1</param-name>
      <param-value>parm1value</param-value>
      <description>description</description>
   </init-param>
</filter>

<filter>
   <filter-name>UpdateParamFilter</filter-name>
   <display-name>UpdateParamFilter</display-name>
   <description>UpdateParamFilter</description>
   <filter-class>com.ai.servlets.paramfilters.UpdateParamFilter</filter-class>
</filter>

<filter-mapping>
   <filter-name>DisplayParamFilter</filter-name>
   <url-pattern>/display/*</url-pattern>
</filter-mapping>    

<filter-mapping>
   <filter-name>UpdateParamFilter</filter-name>
   <url-pattern>/update/*</url-pattern>
</filter-mapping> 

Example of a file upload html file


<form action="/webapp/update/uploadRequestName"
enctype="multipart/form-data" method="post">
<p>
Type some text (if you like):<br>
<input type="text" name="textline" size="30">
</p>
<p>
Please specify a file, or a set of files:<br>
<input type="file" name="datafile" size="40">
</p>
<div>
<input type="submit" value="Send">
</div>
</form>

Notice the "uploadRequestName" how it is specified. This is possible if you configure the above filters only.

Writing the request in the properties file


##set up the pipeline
request.uploadRequestName.classname=com.ai.db.DBPreTranslateArgsMultiRequestExecutor
request.uploadRequestName.request.1=parseUsingCommons
request.uploadRequestName.request.2=ConvertFileItemToBlob
request.uploadRequestName.request.3=SaveBlob

##redirect after success
request.uploadRequestName.redirectURL=/webapp/somepage?a=b

##parsing: fileitem will be checked in as your form field name
request.parseUsingCommons.classname=com.ai.parts.SingleFileUPloadCommonsPart
#the following line is not used. The fileitem is checked
#under the name of the form field
#request.parseUsingCommons.fileuploadFormFieldName=datafile

##convert to blob
request.convertfileitemtoblob.classname=com.ai.parts.FileItemToBlobPart
request.convertfileitemtoblob.fileItemKey=datafile
request.convertfileitemtoblob.fileStreamKey=datafileStream

##save blob
request.saveblob.classname=com.ai.db.ps.PSExecutor2
request.saveblob.db=your-db
request.saveblob.stmt=\
insert into table1 (blobcolumn, filename,filesize,someid)
values (?,?,?,?)
request.saveblob.paramlist=\
datafileStream,blob|datafileStream_filename|datafileStream_length,int|someid,int

Reading the file


downloadfileURL=no-template
downloadfileURL.transformtype=jsp
downloadfile.transform.classname=com.ai.htmlgen.streamers.BlobTransform
downloadfile.transform.blobRequestName=readBlob
downloadfile.transform.blobKey=blobColumnName
downloadfile.transform.filenameKey=filenameColumnName
downloadfile.transform.fileLengthKey=fileLengthKeyColumnName

request.readBlob.classname=com.ai.db.DBRequestExecutor2
request.readBlob.db=somedb
request.readBlob.stmt=slect * from some table where id = {someid}

Global setup for the file uploads

I will put this up tomorrow. I have the details somewhere else.

what build is required for this

Build 24

Caution

This code is in alpha state. email me when you run into issues.