private void UploadFile()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////得到用户要上传的文件名
string strFilePathName = loFile.PostedFile.FileName;
string strFileName = Path.GetFileName(strFilePathName);
int FileLength = loFile.PostedFile.ContentLength;
if(FileLength<=0)
return;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////上传文件
// try
//
// {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////图象文件临时储存Byte数组
Byte[] FileByteArray = new Byte[FileLength];
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////建立数据流对像
Stream StreamObject = loFile.PostedFile.InputStream;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObject.Read(FileByteArray,0,FileLength);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////建立SQL Server链接
// string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
SqlConnection Con = new SqlConnection("server=.;uid=sa;pwd=sa;database=wjoa");
String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image",SqlDbType.Image, FileLength).Value = FileByteArray;
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,2000).Value = loFile.PostedFile.ContentType; //记录文件类型
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////把其它单表数据记录上传
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,2000).Value = tbDescription.Text;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////记录文件长度,读取时使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.Int).Value = FileLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////跳转页面
// Response.Redirect("ShowAll.aspx");
// }
//
// catch(Exception ex)
//
// {
// throw ex;
// }
}
![](/Images/OutliningIndicators/None.gif)
private void ShowImages()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////ID为图片ID
int ImgID = int.Parse(this.TextBox1.Text.Trim());
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////建立数据库连接
SqlConnection Con = new SqlConnection("server=.;uid=sa;pwd=sa;database=wjoa");
String SqlCmd = "SELECT * FROM ImageStore WHERE id = @ImageID";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID;
Con.Open();
SqlDataReader SqlReader = CmdObj.ExecuteReader();
SqlReader.Read();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////设定输出文件类型
Response.ContentType = (string)SqlReader["ImageContentType"];
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**////输出图象文件二进制数制
Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]);
Response.End();
Con.Close();
}
![](/Images/OutliningIndicators/None.gif)
posted on
2006-06-24 10:24
过江
阅读(
520)
评论()
编辑
收藏
举报