ASP.NET中常用功能代码总结(4)——保存图片到XML文件
整理:Terrylee
一.保存图片到XML文件
1
/// <summary>
2
/// 保存图片到XML文件
3
/// </summary>
4
private void UploadImageToXml()
5
{
6
///得到用户要上传的文件名
7
string strFilePathName = loFile.PostedFile.FileName;
8
string strFileName = Path.GetFileName(strFilePathName);
9
int FileLength = loFile.PostedFile.ContentLength;
10
if(FileLength<=0)
11
return;
12
try
13
{
14
///图象文件临时储存Byte数组
15
Byte[] FileByteArray = new Byte[FileLength];
16
17
///建立数据流对像
18
Stream StreamObject = loFile.PostedFile.InputStream;
19
20
///读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
21
StreamObject.Read(FileByteArray,0,FileLength);
22
23
///要打开的文件
24
string fileName = Server.MapPath(".\\WriteXml.xml");
25
26
XmlDocument xmlDoc = new XmlDocument();
27
xmlDoc.Load(fileName);
28
29
///查找<dbGuest>
30
XmlNode root=xmlDoc.SelectSingleNode("dbImage");
31
XmlNodeList xnl=xmlDoc.SelectSingleNode("dbImage").ChildNodes;
32
int nIndex = xnl.Count;
33
34
///以下添加新结点
35
XmlElement xe1=xmlDoc.CreateElement("Image");//创建一个<User>节点
36
37
XmlElement xesub1=xmlDoc.CreateElement("ImageID");
38
39
///设置文本节点
40
xesub1.InnerText=nIndex.ToString();
41
42
///添加到<User>节点中
43
xe1.AppendChild(xesub1);
44
XmlElement xesub2=xmlDoc.CreateElement("ImageContentType");
45
xesub2.InnerText=loFile.PostedFile.ContentType;
46
xe1.AppendChild(xesub2);
47
XmlElement xesub3=xmlDoc.CreateElement("ImageSize");
48
xesub3.InnerText=FileLength.ToString();
49
xe1.AppendChild(xesub3);
50
XmlElement xesub4=xmlDoc.CreateElement("ImageDescription");
51
xesub4.InnerText=tbDescription.Text;
52
xe1.AppendChild(xesub4);
53
XmlElement xesub5=xmlDoc.CreateElement("ImageData");
54
xesub5.InnerText= Convert.ToBase64String(FileByteArray);
55
xe1.AppendChild(xesub5);
56
57
///添加到<dbGuest>节点中
58
root.AppendChild(xe1);
59
xmlDoc.Save(fileName);
60
61
Response.Redirect("ShowAllImg.aspx");
62
}
63
catch(Exception ex)
64
{
65
throw ex;
66
}
67
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

二.从XML中读取图片数据
1
/// <summary>
2
/// 从XML中读取图片
3
/// </summary>
4
/// <param name="ImageID">图片ID</param>
5
private void ReadImageFromXml(string ImageID)
6
{
7
///ID为图片ID
8
int ImgID = Convert.ToInt32(ImageID);
9
10
///要打开的文件
11
string fileName = Server.MapPath(".\\WriteXml.xml");
12
13
XmlDocument xmlDoc = new XmlDocument();
14
xmlDoc.Load(fileName);
15
XmlNodeList node = xmlDoc.SelectSingleNode("//Image[ImageID='"+ImgID.ToString()+"']").ChildNodes;
16
if(node!=null)
17
{
18
string strType = node.Item(1).InnerText;
19
string strData =node.Item(4).InnerText;
20
int nSize = int.Parse(node.Item(2).InnerText);
21
22
///设定输出文件类型
23
Response.ContentType = strType;
24
25
///输出图象文件二进制数制
26
Response.OutputStream.Write(Convert.FromBase64String(strData), 0, nSize);
27
Response.End();
28
29
//也可以保存为图像
30
//FileStream fs = new FileStream(@"C:\aa.BMP", FileMode.OpenOrCreate, FileAccess.Write);
31
//fs.Write((Convert.FromBase64String(strData), 0,nSize);
32
//fs.Close();
33
}
34
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34
