博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图像保存到XML文件和从XML中取出图像显示
阅读量:6997 次
发布时间:2019-06-27

本文共 2438 字,大约阅读时间需要 8 分钟。

一、保存到XML文件
None.gif
//
得到用户要上传的文件名
None.gif
string strFilePathName = loFile.PostedFile.FileName;
None.gif
string strFileName = Path.GetFileName(strFilePathName);
None.gif
int FileLength = loFile.PostedFile.ContentLength;
None.gif
if(FileLength<=0)
None.gif
return;
None.gif
try
ExpandedBlockStart.gif
ContractedBlock.gif
dot.gif {
InBlock.gif Byte[] FileByteArray =
new Byte[FileLength];
//
图象文件临时储存Byte数组
InBlock.gif
Stream StreamObject = loFile.PostedFile.InputStream;
//
建立数据流对像
InBlock.gif
//
读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
InBlock.gif
StreamObject.Read(FileByteArray,0,FileLength);
InBlock.gif
string fileName = Server.MapPath(".\\WriteXml.xml");
//
要打开的文件
InBlock.gif
InBlock.gif XmlDocument xmlDoc =
new XmlDocument();
InBlock.gif xmlDoc.Load(fileName);
InBlock.gif XmlNode root=xmlDoc.SelectSingleNode("dbImage");
//
查找<dbGuest>
InBlock.gif
XmlNodeList xnl=xmlDoc.SelectSingleNode("dbImage").ChildNodes;
InBlock.gif
int nIndex = xnl.Count;
InBlock.gif
//
以下添加新结点
InBlock.gif
XmlElement xe1=xmlDoc.CreateElement("Image");
//
创建一个<User>节点
InBlock.gif
XmlElement xesub1=xmlDoc.CreateElement("ImageID");
InBlock.gif xesub1.InnerText=nIndex.ToString();
//
设置文本节点
InBlock.gif
xe1.AppendChild(xesub1);
//
添加到<User>节点中
InBlock.gif
XmlElement xesub2=xmlDoc.CreateElement("ImageContentType");
InBlock.gif xesub2.InnerText=loFile.PostedFile.ContentType;
InBlock.gif xe1.AppendChild(xesub2);
InBlock.gif XmlElement xesub3=xmlDoc.CreateElement("ImageSize");
InBlock.gif xesub3.InnerText=FileLength.ToString();
InBlock.gif xe1.AppendChild(xesub3);
InBlock.gif XmlElement xesub4=xmlDoc.CreateElement("ImageDescription");
InBlock.gif xesub4.InnerText=tbDescription.Text;
InBlock.gif xe1.AppendChild(xesub4);
InBlock.gif XmlElement xesub5=xmlDoc.CreateElement("ImageData");
InBlock.gif xesub5.InnerText= Convert.ToBase64String(FileByteArray);
InBlock.gif xe1.AppendChild(xesub5);
InBlock.gif
InBlock.gif
InBlock.gif root.AppendChild(xe1);
//
添加到<dbGuest>节点中
InBlock.gif
xmlDoc.Save(fileName);
InBlock.gif
InBlock.gif Response.Redirect("ShowAllImg.aspx");
ExpandedBlockEnd.gif }
None.gif
catch
ExpandedBlockStart.gif
ContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif }
二、从XML文件中取出显示
None.gif
int ImgID = Convert.ToInt32(Request.QueryString["ID"]);
//
ID为图片ID
None.gif
//
建立数据库链接
None.gif
string fileName = Server.MapPath(".\\WriteXml.xml");
//
要打开的文件
None.gif
None.gif XmlDocument xmlDoc =
new XmlDocument();
None.gif xmlDoc.Load(fileName);
None.gif XmlNodeList node = xmlDoc.SelectSingleNode("//Image[ImageID='"+ImgID.ToString()+"']").ChildNodes;
None.gif
if(node!=
null)
ExpandedBlockStart.gif
ContractedBlock.gif
dot.gif {
InBlock.gif
string strType = node.Item(1).InnerText;
InBlock.gif
string strData =node.Item(4).InnerText;
InBlock.gif
int nSize =
int.Parse(node.Item(2).InnerText);
InBlock.gif
InBlock.gif Response.ContentType = strType;
//
设定输出文件类型
InBlock.gif
//
输出图象文件二进制数制
InBlock.gif
Response.OutputStream.Write(Convert.FromBase64String(strData), 0, nSize);
InBlock.gif Response.End();
InBlock.gif
//
也可以保存为图像
InBlock.gif
//
FileStream fs = new FileStream(@"C:\aa.BMP", FileMode.OpenOrCreate, FileAccess.Write);
InBlock.gif
//
fs.Write((Convert.FromBase64String(strData), 0,nSize);
InBlock.gif
//
fs.Close();
ExpandedBlockEnd.gif
}
本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/10/17/256605.html,如需转载请自行联系原作者
你可能感兴趣的文章
本地存储
查看>>
react-native环境配置入坑指南.
查看>>
使用qemu
查看>>
小试下新博客,一个列传行的SQL
查看>>
带你一分钟理解闭包--js面向对象编程
查看>>
MySql基本使用方法
查看>>
LAME的“命令行”
查看>>
PyQt5学习-day1 -4 退出按钮
查看>>
使用Parallel.Invoke并行你的代码
查看>>
口袋笔记VS松鼠笔记
查看>>
silverlight 将chart图倒入到excel
查看>>
LeetCode – Refresh – Word Search
查看>>
ADO.NET笔记——使用Connection连接数据库,使用Command对象的ExecuteReader()方法创建DataReader对象返回多行数据...
查看>>
HDU sum问题
查看>>
C语言基础知识汇总
查看>>
数字高程模型和地图——thematicmapping.org译文(一)
查看>>
8-5 泛型类型擦除
查看>>
正文处理命令及tar命令
查看>>
实习第三周小记-----生活在于经历 分类: 程序人生 ...
查看>>
将excel中的数据转为json格式
查看>>