一、保存到XML文件
//得到用户要上传的文件名
string strFilePathName = loFile.PostedFile.FileName;
string strFileName = Path.GetFileName(strFilePathName);
int FileLength = loFile.PostedFile.ContentLength;
if(FileLength<=0)
return;
try
{
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObject = loFile.PostedFile.InputStream; //建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObject.Read(FileByteArray,0,FileLength);
string fileName = Server.MapPath(".\\WriteXml.xml"); //要打开的文件
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
XmlNode root=xmlDoc.SelectSingleNode("dbImage");//查找<dbGuest>
XmlNodeList xnl=xmlDoc.SelectSingleNode("dbImage").ChildNodes;
int nIndex = xnl.Count;
//以下添加新结点
XmlElement xe1=xmlDoc.CreateElement("Image");//创建一个<User>节点
XmlElement xesub1=xmlDoc.CreateElement("ImageID");
xesub1.InnerText=nIndex.ToString();//设置文本节点
xe1.AppendChild(xesub1);//添加到<User>节点中
XmlElement xesub2=xmlDoc.CreateElement("ImageContentType");
xesub2.InnerText=loFile.PostedFile.ContentType;
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("ImageSize");
xesub3.InnerText=FileLength.ToString();
xe1.AppendChild(xesub3);
XmlElement xesub4=xmlDoc.CreateElement("ImageDescription");
xesub4.InnerText=tbDescription.Text;
xe1.AppendChild(xesub4);
XmlElement xesub5=xmlDoc.CreateElement("ImageData");
xesub5.InnerText= Convert.ToBase64String(FileByteArray);
xe1.AppendChild(xesub5);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
root.AppendChild(xe1);//添加到<dbGuest>节点中
xmlDoc.Save(fileName);
Response.Redirect("ShowAllImg.aspx");
}
catch
{
}二、从XML文件中取出显示
int ImgID = Convert.ToInt32(Request.QueryString["ID"]); //ID为图片ID
//建立数据库链接
string fileName = Server.MapPath(".\\WriteXml.xml"); //要打开的文件
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
XmlNodeList node = xmlDoc.SelectSingleNode("//Image[ImageID='"+ImgID.ToString()+"']").ChildNodes;
if(node!=null)
{
string strType = node.Item(1).InnerText;
string strData =node.Item(4).InnerText;
int nSize = int.Parse(node.Item(2).InnerText);
![](/Images/OutliningIndicators/InBlock.gif)
Response.ContentType = strType;//设定输出文件类型
//输出图象文件二进制数制
Response.OutputStream.Write(Convert.FromBase64String(strData), 0, nSize);
Response.End();
//也可以保存为图像
// FileStream fs = new FileStream(@"C:\aa.BMP", FileMode.OpenOrCreate, FileAccess.Write);
// fs.Write((Convert.FromBase64String(strData), 0,nSize);
// fs.Close();
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)