ASP.NET中常用功能代码总结(3)——上传图片到数据库
ASP.NET中常用功能代码总结(3)——上传图片到数据库
整理:Terrylee
一.上传图片到数据库
1
/// <summary>
2
/// 上传图片
3
/// </summary>
4
private void UploadFile()
5
{
6
///得到用户要上传的文件名
7
string strFilePathName = loFile.PostedFile.FileName;
8
string strFileName = Path.GetFileName(strFilePathName);
9
int FileLength = loFile.PostedFile.ContentLength;
10![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
if(FileLength<=0)
12
return;
13![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
///上传文件
15
try
16
{
17
18
///图象文件临时储存Byte数组
19
Byte[] FileByteArray = new Byte[FileLength];
20![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
///建立数据流对像
22
Stream StreamObject = loFile.PostedFile.InputStream;
23![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
///读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
25
StreamObject.Read(FileByteArray,0,FileLength);
26![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
///建立SQL Server链接
28
string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
29
SqlConnection Con = new SqlConnection(strCon);
30
String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";
31
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
32
CmdObj.Parameters.Add("@Image",SqlDbType.Binary, FileLength).Value = FileByteArray;
33
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,50).Value = loFile.PostedFile.ContentType; //记录文件类型
34
35
///把其它单表数据记录上传
36
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).Value = tbDescription.Text;
37
38
///记录文件长度,读取时使用
39
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).Value = FileLength;
40
Con.Open();
41
CmdObj.ExecuteNonQuery();
42
Con.Close();
43![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
///跳转页面
45
Response.Redirect("ShowAll.aspx");
46
}
47
catch(Exception ex)
48
{
49
throw ex;
50
}
51
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
39
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
49
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
二.从数据库中读取图片
1
/// <summary>
2
/// 显示图片
3
/// </summary>
4
private void ShowImages()
5
{
6
///ID为图片ID
7
int ImgID = Convert.ToInt32(Request.QueryString["ID"]);
8
9
///建立数据库连接
10
string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
11
SqlConnection Con = new SqlConnection(strCon);
12
String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = @ImageID";
13
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
14
CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID;
15
16
Con.Open();
17
SqlDataReader SqlReader = CmdObj.ExecuteReader();
18
SqlReader.Read();
19![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
///设定输出文件类型
21
Response.ContentType = (string)SqlReader["ImageContentType"];
22
23
///输出图象文件二进制数制
24
Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]);
25
Response.End();
26![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
Con.Close();
28
}
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
posted on 2005-12-02 11:16 Chandler.C 阅读(749) 评论(3) 编辑 收藏 举报