///
/// 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.Stream docStream = inputFile.PostedFile.InputStream;
// 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
byte[] contents = new byte[intDocLen];
docStream.Read(contents, 0, intDocLen);
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
// rec.ID = "5"
rec.${Attachment Field} = contents;
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.");
}
}
|