<script runat="server" LANGUAGE="C#">
void Page_Load(Object Source, EventArgs E)
{
// Create an instance of AspUpload object
IUploadManager objUpload;
objUpload = new UploadManager();
String strPath = Server.MapPath(".");
// Save returns the number of uploaded files
int nCount = objUpload.Save(strPath, Missing.Value, Missing.Value);
if( nCount == 0 )
{
txtMsg.InnerHtml = "No images selected.";
return;
}
// Obtain File object representing uploaded file
IUploadedFile objFile;
objFile = objUpload.Files.Item(1);
// Is this a valid image file?
if( objFile.ImageType != "UNKNOWN" )
{
// Create instance of AspJpeg object
IASPJpeg objJpeg;
objJpeg = new ASPJpeg();
// Open uploaded file
objJpeg.Open( objFile.Path );
// Resize image according to "scale" option.
// We cannot use Request.Form, so we use Upload.Form instead.
int nScale = int.Parse(objUpload.Form.Item("scale").Value);
objJpeg.Width = objJpeg.OriginalWidth * nScale / 100;
objJpeg.Height = objJpeg.OriginalHeight * nScale / 100;
String strSavePath = strPath + "\\small_" + objFile.ExtractFileName();
// AspJpeg always generates thumbnails in JPEG format.
// If the original file was not a JPEG, append .JPG extension.
if( strSavePath.ToUpper().Substring( strSavePath.Length - 3) != "JPG" )
{
strSavePath += ".jpg";
}
objJpeg.Save( strSavePath );
// Save both images in the database along with description.
String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../db/aspjpeg.mdb");
OleDbConnection myConnection = new OleDbConnection(strConn);
myConnection.Open();
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter ("select * from images", myConnection);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill( myDataSet, "images" );
DataTable tblImages = myDataSet.Tables["images"];
DataRow rowImage;
// Add a new row
rowImage = tblImages.NewRow();
tblImages.Rows.Add( rowImage );
rowImage.BeginEdit();
// Save original image and thumbnail in the database table.
rowImage["original_image"] = objFile.Binary;
IUploadedFile objThumb = objUpload.OpenFile( strSavePath );
rowImage["thumbnail"] = objThumb.Binary;
rowImage["description"] = objUpload.Form.Item("description").Value;
rowImage.EndEdit();
// Without this line, Update will fail.
OleDbCommandBuilder myCB = new OleDbCommandBuilder(myDataAdapter);
myDataAdapter.Update( myDataSet, "images" );
myConnection.Close();
txtMsg.InnerHtml = "Success!";
}
else
{
txtMsg.InnerHtml = "This is not a valid image.";
}
}
</script>
|