protected System.Web.UI.HtmlControls.HtmlInputFile ImageToUpload;
string ContentType = ImageToUpload.PostedFile.ContentType;
string str = ImageToUpload.PostedFile.FileName;
/// <summary>
/// Length holds the size of the file that was added to the ImageToUpload control </summary>
int Length = System.Convert.ToInt32(ImageToUpload.PostedFile.InputStream.Length);
if (Length == 0)
{
Response.Write("<script language=javascript>alert('图片太小了')></script>");
return;
}
/// <summary>
/// Content will hold the image. It is a byte array of size Length </summary>
byte[] Content = new byte[Length];
/// <summary>
/// The Read method is used to read the file from the ImageToUpload control </summary>
ImageToUpload.PostedFile.InputStream.Read(Content,0,Length);
/// <summary>
/// Open a connection to the SQL Server </summary>
SqlConnection Connection = new SqlConnection("server=localhost;uid=sa;pwd=;database=ImageUpload");
/// <summary>
/// The SqlCommand will be used to insert the image into the Images table </summary>
SqlCommand Command = new SqlCommand("INSERT Into Images(Description, ImageFile, ImageSize, ImageType) Values(@Description, @ImageFile, @ImageSize, @ImageType)", Connection);
/// <summary>
/// The Description parameter is used to add the image file description to the database
SqlParameter imageDescriptionParameter = new SqlParameter("@Description", SqlDbType.NVarChar);
imageDescriptionParameter.Value = imageDescription.Text;
Command.Parameters.Add(imageDescriptionParameter);
/// <summary>
/// The ImageFile parameter is used to add the image file to the database
SqlParameter imageFileParameter = new SqlParameter("@ImageFile", SqlDbType.Image);
imageFileParameter.Value = Content;
Command.Parameters.Add(imageFileParameter);
/// <summary>
/// The ImageSize parameter is used to add the image file size to the database
SqlParameter imageSizeParameter = new SqlParameter("@ImageSize", SqlDbType.Int);
imageSizeParameter.Value = Length;
Command.Parameters.Add(imageSizeParameter);
/// <summary>
/// The ImageType parameter is used to add the image file type to the database
SqlParameter imageTypeParameter = new SqlParameter("@ImageType", SqlDbType.NVarChar);
imageTypeParameter.Value = ContentType;
Command.Parameters.Add(imageTypeParameter);
/// <summary>
/// Open the connection in order to retrieve the record </summary>
Connection.Open();
/// <summary>
/// The SQL statement is executed. ExecuteNonQuery is used since no records
/// will be returned. </summary>
Command.ExecuteNonQuery();
/// <summary>
/// The connection is closed </summary>
Connection.Close();