net9:图片变成二进制流存入XML文档,从XML文档中读出图片以及从XML文档中读取并创建图片文件
2021-06-29 12:03
标签:response sel count 工具 click font -- write base 原文发布时间为:2008-08-10 —— 来源于本人的百度文章 [由搬家工具导入] fileToXml类: using System; using System.IO; /// XmlElement root = dom.CreateElement("File"); XmlElement father = dom.CreateElement("Image"); XmlElement guid = dom.CreateElement("Guid"); XmlElement imgData = dom.CreateElement("imgData"); dom.Save(xmlpath); dom.Load(xmlpath); XmlElement guid = dom.CreateElement("Guid"); XmlElement imgData = dom.CreateElement("imgData"); dom.Save(xmlpath); using System; using System.Xml; public partial class Default3 : System.Web.UI.Page } Session["gid"] = gid; net9:图片变成二进制流存入XML文档,从XML文档中读出图片以及从XML文档中读取并创建图片文件 标签:response sel count 工具 click font -- write base 原文地址:http://www.cnblogs.com/handboy/p/7141624.html
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.Xml;
/// fileToXml 的摘要说明
///
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);
dom.AppendChild(root);
root.AppendChild(father);
guid.InnerText = id.ToString();
father.AppendChild(guid);
imgData.InnerText = Convert.ToBase64String(fileBytes);
father.AppendChild(imgData);
}
else
{
XmlNode root = dom.SelectSingleNode("File");
XmlElement father = dom.CreateElement("Image");
root.AppendChild(father);
guid.InnerText = id.ToString();
father.AppendChild(guid);
imgData.InnerText = Convert.ToBase64String(fu.FileBytes);//这是利用fileupload控件本身的方法获取二进制流
father.AppendChild(imgData);
}
}
}
}
------------------------------
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.IO;
{
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);
}
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 {
string imgdata = 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 {
string imgdata = 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文档中读取并创建图片文件
文章链接:http://soscw.com/essay/99352.html