Can I assign an external file to a FileUpload control during submission of webform?


#1

Issue

Is it possible to assign an external file to a FileUpload control during the submission of a web form?

Background

This applies to the condition where you need to attach a file to the file upload control without user input. It is applicable to WorkflowGenFileUpload control (in version 5.7 and earlier) and FileUpload settings Advanced mode (in version 6 and later) in SimpleMode only. The standard .NET FileUpload control does not have the ability to display uploaded files. As well, this can only be done during the submission of a web form.

Solution

Since the WorkflowGenPage engine does NOT create a new node in the FormData XML file if there is no file uploaded via a FileUpload Control, the file must be associated to the control manually. This procedure must be done AFTER the call of SubmitToWorkflow().

Assuming there is a WorkflowGenFileUpload control with an ID REQUEST_ATTACH:

//--- trigger SubmitToWorkflow to get ready for the closure of an action
SubmitToWorkflow(this.FormData);
//---- associate file to the file upload control after SubmitToWorkflow()---
//---- upload FormData with all the form field controls
SaveFieldsData(this.FormData);
//---- make sure the FileUpload control is available in FormData
if (!this.FormData.Tables[0].Columns.Contains("REQUEST_ATTACH"))
{ 
 this.FormData.Tables[0].Columns.Add("REQUEST_ATTACH"); 
}
//----- copy file from source to the UPLOAD/REQUEST_ATTACH folder
string CopiedFileName = "[your file name]";
string destFolder = StoragePath + "\\UPLOAD\\REQUEST_ATTACH\\";
string destFile = System.IO.Path.Combine(destFolder, CopiedFileName);
//---- make sure the destination folder is ready ---
if (!System.IO.Directory.Exists(destFolder)) { 
  System.IO.Directory.CreateDirectory(destFolder); 
}
//---- copy the file destination folder
string sourceFile = "[the source of your file]";
System.IO.File.Copy(sourceFile, destFile, true);
//--- update the node for REQUEST_ATTACH ---
this.FormData.Tables[0].Rows[0]["REQUEST_ATTACH"] = "UPLOAD\\REQUEST_ATTACH\\" + CopiedFileName;
//--- call SaveFormData to update the FormData Xml file without adding the schema
informationSaveFormData(this.FormData, false);