首页 > 代码库 > net9:图片变成二进制流存入XML文档,从XML文档中读出图片以及从XML文档中读取并创建图片文件
net9:图片变成二进制流存入XML文档,从XML文档中读出图片以及从XML文档中读取并创建图片文件
原文发布时间为:2008-08-10 —— 来源于本人的百度文章 [由搬家工具导入]
fileToXml类:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
/// <summary>
/// fileToXml 的摘要说明
/// </summary>
public class fileToXml
{
public fileToXml()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static void toXml(FileUpload fu, string xmlpath, Guid id)
{
if (fu.HasFile)
{
string fpath = fu.FileName;
int fileLength = fu.PostedFile.ContentLength;
Byte[] fileBytes = new Byte[fileLength];
Stream strm = fu.PostedFile.InputStream;
strm.Read(fileBytes, 0, fileLength);
XmlDocument dom = new XmlDocument();
if (!File.Exists(xmlpath))
{
XmlDeclaration xdec = dom.CreateXmlDeclaration("1.0", "utf-8", null);
dom.AppendChild(xdec);
XmlElement root = dom.CreateElement("File");
dom.AppendChild(root);
XmlElement father = dom.CreateElement("Image");
root.AppendChild(father);
XmlElement guid = dom.CreateElement("Guid");
guid.InnerText = id.ToString();
father.AppendChild(guid);
XmlElement imgData = http://www.mamicode.com/dom.CreateElement("imgData");
imgData.InnerText = Convert.ToBase64String(fileBytes);
father.AppendChild(imgData);
dom.Save(xmlpath);
}
else
{
dom.Load(xmlpath);
XmlNode root = dom.SelectSingleNode("File");
XmlElement father = dom.CreateElement("Image");
root.AppendChild(father);
XmlElement guid = dom.CreateElement("Guid");
guid.InnerText = id.ToString();
father.AppendChild(guid);
XmlElement imgData = http://www.mamicode.com/dom.CreateElement("imgData");
imgData.InnerText = Convert.ToBase64String(fu.FileBytes);//这是利用fileupload控件本身的方法获取二进制流
father.AppendChild(imgData);
dom.Save(xmlpath);
}
}
}
}
------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string xmlpath = Server.MapPath("~/App_Data/imgToBry.xml");
Guid gid = Guid.NewGuid();
fileToXml.toXml(FileUpload1, xmlpath, gid);
Session["gid"] = gid;
}
protected void Button2_Click(object sender, EventArgs e)
{
XmlDocument dom = new XmlDocument();
dom.Load(Server.MapPath("~/App_Data/imgToBry.xml"));
XmlNodeList xnl = dom.SelectSingleNode("//Image[Guid='" + Session["gid"].ToString() + "']").ChildNodes;
for (int i = 0; i < xnl.Count; i++) ;
{
string imgdata = http://www.mamicode.com/xnl.Item(1).InnerText;
Response.OutputStream.Write(Convert.FromBase64String(imgdata),0,imgdata.Length);
Response.End();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
XmlDocument dom = new XmlDocument();
dom.Load(Server.MapPath("~/App_Data/imgToBry.xml"));
XmlNodeList xnl = dom.SelectSingleNode("//Image[Guid='" + Session["gid"].ToString() + "']").ChildNodes;
for (int i = 0; i < xnl.Count; i++) ;
{
string imgdata = http://www.mamicode.com/xnl.Item(1).InnerText;
FileStream fs = new FileStream(Server.MapPath("~/xml.gif"), FileMode.Create, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(Convert.FromBase64String(imgdata));
bw.Close();
fs.Close();
}
Image1.ImageUrl="~/xml.gif";
}
}
net9:图片变成二进制流存入XML文档,从XML文档中读出图片以及从XML文档中读取并创建图片文件