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

How to upload files: one annotated approach

Global Definitions for file upload


#############################################
#Global file definitions
#############################################
aspire.global-defaults.fileupload.MaxMemorySizeInKb=1000
aspire.global-defaults.fileupload.maxRequestSizeInMb=10
aspire.global-defaults.fileupload.tempDirectory=c:\\work

A multistep request to upload the file


#############################################
#Test File Upload
#############################################
request.DocFUL.classname=com.ai.data.PreTranslateArgsMultiRequestExecutor
request.DocFUL.request.1=DFUL.parsemultipart
request.DocFUL.request.2=DFUL.FULCopyFile
request.DocFUL.request=DFUL.savefileInDatabase

#redirect back to the display page
request.DocFUL.redirectURL=\
/akc/display?url=UploadMPURL&reportId={reportId}&ownerUserId={ownerUserId}

#step1: parse multipart using commons


request.DFUL.parsemultipart.classname=com.ai.parts.SingleFileUPloadCommonsPart
#The following is read but it is not used 
#request.testFileUpload.fileUploadFormFieldName=datafile

#step2: Copy file using the preddefined part FULCopyFilePart


request.DFUL.FULCopyFile.classname=com.ai.parts.FULCopyFilePart
request.DFUL.FULCopyFile.targetDirectory=\
c:\\work\\testdir\\{profile_user}\\documentfiles\\{reportId}
request.DFUL.FULCopyFile.filename={{datafile_filename}}
request.DFUL.FULCopyFile.fileuploadFormFieldName=datafile

#step3: Save file name, size etc, in database for better manipulation


request.DFUL.savefileInDatabase.classname=com.ai.db.DBRequestExecutor2
request.DFUL.savefileInDatabase.db=reportsDB
request.DFUL.savefileInDatabase.query_type=update
request.DFUL.savefileInDatabase.stmt=\
insert into file_attachments (\
   file_id \
   ,filename \
   ,filesize \
   ,owner_user_id \
   ,last_updated_by \
   ,last_updated_on) \
values(\
   {reportId} \
   ,{datafile_filename.quote} \
   ,{datafile_filesize} \
   ,{profile_user.quote} \
   ,{profile_user.quote} \
   ,Now() \
)

Here is the display page to list files uploaded


###################################
# UploadMPURL: reportId, ownerUserId
###################################
#basics
UploadMPURL=aspire:\\reports\\upload\\upload-note-mp.html
UploadMPURL.dataRequestName=GetFileItemWithAttachments
UploadMPURL.masterPageRequestName=GetMasterPage

#############################################
#GetFileItemWithAttachments(reportId)
#############################################
request.GetFileItemWithAttachments.className=\
com.ai.htmlgen.DBHashTableFormHandler1
#children specification for XML support
GetFileItemWithAttachments.loopnames=attachmentsloop
#this is needed to eliminate the table from being
#displayed when there is no data
GetFileItemWithAttachments.attachmentsloop.eliminateLoop=yes

request.GetFileItemWithAttachments.maindatarequest.className=\
com.ai.db.DBRequestExecutor2
request.GetFileItemWithAttachments.maindatarequest.db=reportsDB
request.GetFileItemWithAttachments.maindatarequest.stmt=\
\
select * \
from reports r, sql_statements st, filed_items fi, folders f \
where 1=1 \
and r.report_content_id = st.statement_id \
and r.report_id = {reportId} \
and r.report_id = fi.item_id \
and fi.folder_id = f.folder_id

#Retrieve the list of files
#see the select statement at the bottom of this section
request.GetFileItemWithAttachments.attachmentsloop.class_request.className=\
com.ai.htmlgen.GenericTableHandler6
request.GetFileItemWithAttachments.attachmentsloop.query_request.className=\
com.ai.db.DBRequestExecutor2
request.GetFileItemWithAttachments.attachmentsloop.query_request.db=reportsDB
request.GetFileItemWithAttachments.attachmentsloop.query_request.stmt=\
select * from file_attachments \
where file_id={reportId}

How you display the web form to upload


<form name="FileUploadForm" action="javascript:uploadFile()" method="POST"
   enctype="multipart/form-data" method="post">
<input type="hidden" name="reportId" value="{{reportId}}"/>
<input type="hidden" name="ownerUserId" value="{{ownerUserId}}"/>
<input type="hidden" name="date"/>

<!-- file type -->
<p>
Choose your file
</p><p>
<input type="file" name="datafile" size="50"/>
</p>

<!-- submit -->
<p>
<input type="submit" value="attach"/>
</p>
</form>

How to display a table worth of files

Previous files


<!--RLF_TAG BGN_LOOP attachmentsloop -->
<p>{{aspire_rownum}}) {{filename}}, {{last_updated_on}}, {{last_updated_by}}</p>
<!--RLF_TAG END_LOOP attachmentsloop -->

How to display a message when there are no files


<!--RLF_TAG BGN_IF aspire.loops.attachmentsloop=false if2 -->
<p>No File Attachments Available for this document.</p>
<!--RLF_TAG END_IF aspire.loops.attachmentsloop=false if2 -->

The submit function


function uploadFile()
{
   //alert("hello iam here");
   var vAction = "/akc/update/DocFUL";
   document.FileUploadForm.action=vAction;
   document.FileUploadForm.submit();
   //alert("submitted");
}