Attachments
Upload a Text Attachment to a New Record

Description
This customization shows how to upload a text attachment to a text field in a record (such as Oracle CLOB field for example).
Variables
Upload Button Control
Select the upload button
Table Name
Select the table having your destination record type
Attachment filename
Select the field where the name of the file with attachment will be placed
Attachment Field
Select the field where the attachment will be placed
Applies to
P_Upload Button Control class
Code
 
/// 
/// Click handler for Button uploads an attachment to a 
/// new record in database.
///    
public override void ${Upload Button Control}_Click(Object sender , EventArgs args)
{
    System.Web.UI.HtmlControls.HtmlInputFile inputFile;   
    inputFile = (System.Web.UI.HtmlControls.HtmlInputFile)((BaseApplicationPage)this.Page).FindControlRecursively("inputFile");
    if ((!(inputFile.PostedFile == null) && (inputFile.PostedFile.ContentLength > 0)))
    {
        // Get the name of the file to be uploaded        
        string path = inputFile.PostedFile.FileName;
        int LastIndex = path.LastIndexOf("\\");
        string fileName = path.Substring((LastIndex + 1));
        int intDocLen = inputFile.PostedFile.ContentLength;
        System.IO.StreamReader txtStream = new System.IO.StreamReader(path) ;
     
        // If you are using Access DB, then contents have to be of OLE type
        // If SQL server then contents have to be of image type
        string mystr="";
        try 
        {
            DbUtils.StartTransaction();
            
            // Create a new record.
            ${${Table Name}RecordClassName} rec = new ${${Table Name}RecordClassName}();

            // Populate the new record with attachment and other information.
            // Where ID is the primary key need to assign ID, like : rec.ID = "5"
			// Sometimes other fileds have to be assigned too, depending on the record settings
			while((mystr=txtStream.ReadLine())!=null) { rec.${Attachment Field} = rec.${Attachment Field} + mystr +"\n"; }
            rec.${Attachment filename} = fileName;

            // Save the new record.
            rec.Save();
            DbUtils.CommitTransaction();
            this.Page.Response.Write("The file has been uploaded to DB.");
        }
        catch (Exception exc)
        {
            this.Page.Response.Write(("Error: " + exc.Message));
            DbUtils.RollBackTransaction();
        }
        finally 
        {
            DbUtils.EndTransaction();
        }
    }
    else 
    {
        this.Page.Response.Write("Please select a file to upload.");
    }
}
     


  Privacy Statement