原文发布时间为: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 = 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 = 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 = 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 = 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文档中读取并创建图片文件的更多相关文章

  1. 【转载】C#将图片以二进制流的方式存入数据库

    在C#开发应用程序的过程中,图片一般会存放在文件系统中,当然图片也可以二进制的方式存放到数据库中,不过一般不建议存放在数据库中,因为图片占用的空间还是挺大的,特殊情况下可以考虑将图片存在数据.此文将介 ...

  2. ASP.Net将图片以二进制方式存入数据库,并读取

    把图片转换成二进制--把二进制转换成图片 private void button1_Click(object sender, EventArgs e) { string path = this.tex ...

  3. php读取图片成二进制流输出

    header( "Content-type: image/jpeg");$PSize = filesize('1.jpg');$picturedata = fread(fopen( ...

  4. 【转载】C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte

    C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte 转载:http://www.itdos.com/Mvc/20150302/0741255.htm ...

  5. 【转载】C#将图片转换为二进制流调用

    在C#中可以使用MemoryStream类.BinaryFormatter类等来操作图片,将图片读取到二进制数据流中,最终转成二进制数据流进行调用,详细的实现如下方法所示. private byte[ ...

  6. 使用C#向Sql Sever中存取网络图片和本地图片(二进制流的形式)

    先是做普通的,存储我们本地的图片,将它转化为二进制流存储到数据库对应的表中. 代码如下: string path = "../../A.jpg"; FileStream fs = ...

  7. [转] js实现对图片的二进制流md5计算

    //计算图片md5 function img_MD5(img_path,callback) { plus.io.resolveLocalFileSystemURL(img_path, function ...

  8. Unity C#图片转换二进制流、字符串互转

    图片转二进制流转换图片互转 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...

  9. net9:图片文件转换成二进制流存入SQL数据库,以及从数据库中读取二进制流输出文件

    原文发布时间为:2008-08-10 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

随机推荐

  1. HTML5中Web存储

    HTML5 中web存储是一个比cookies更好的一个本地存储方式. 那么什么是HTML5存储呢? 使用HTML5可以在本地存储用户浏览的数据,HTML5技术没有出来之前是使用cookies进行本地 ...

  2. HTML5<aside>元素

    HTML5<aside>元素用来定义页面文档中主区域内容之外的内容,但之外的内容是与主区域内容相关的. 实例: <article> <h1>这个页面是我开始用htm ...

  3. The expected type was 'System.Int64' but the actual value was null.”

    System.InvalidOperationException:“An exception occurred while reading a database value for property ...

  4. NOIP模拟赛 机器人

    [题目描述] 早苗入手了最新的Gundam模型.最新款自然有着与以往不同的功能,那就是它能够自动行走,厉害吧. 早苗的新模型可以按照输入的命令进行移动,命令包括‘E’.‘S’.‘W’.‘N’四种,分别 ...

  5. NOIP模拟赛 篮球比赛2

    篮球比赛2(basketball2.*) 由于Czhou举行了众多noip模拟赛,也导致放学后篮球比赛次数急剧增加.神牛们身体素质突飞猛进,并且球技不断精进.这引起了体育老师彩哥的注意,为了给校篮球队 ...

  6. 初识Mysql之基本简单语法总结

    一.  DDL(data definition language)语句:数据定义语言. 这些语句定义了不同的数据段.数据库.表.列.索引等数据库对象.常用语句关键字:create.drop.alter ...

  7. 编译-LAMP基于fastcgi

    前言 最近没更新新篇幅了,今天就来点干活,过多的也不说了下面着手干!干!干! 准备环境 centos7.5 apr-1.6.3.tar.gz  apr-util-1.6.1.tar.gz      h ...

  8. nginx url rewrite break和last的区别

    break 将重写的URI作为一个新的URI,在本块中继续处理,将重写后 的地址在当前location块中处理,不会将新的URI转向到其他location块中 last,终止继续在本location块 ...

  9. c# 输出不同时间的格式

    C#时间/日期格式大全,C#时间/日期函数大全 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2005-6-6 14:33:34 如果要换成成200506,06-2005,2005-6- ...

  10. JAVA基础篇—Servlet小结

    一.get请求和post请求的区别: 1.get请求是通过url传递参数,post请求是通过请求体传递参数的 2.get请求最多允许传递255个字符,对长度有限制,所以数据比较大的时候我们使用post ...