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 = 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文档中读取并创建图片文件的更多相关文章
- 【转载】C#将图片以二进制流的方式存入数据库
在C#开发应用程序的过程中,图片一般会存放在文件系统中,当然图片也可以二进制的方式存放到数据库中,不过一般不建议存放在数据库中,因为图片占用的空间还是挺大的,特殊情况下可以考虑将图片存在数据.此文将介 ...
- ASP.Net将图片以二进制方式存入数据库,并读取
把图片转换成二进制--把二进制转换成图片 private void button1_Click(object sender, EventArgs e) { string path = this.tex ...
- php读取图片成二进制流输出
header( "Content-type: image/jpeg");$PSize = filesize('1.jpg');$picturedata = fread(fopen( ...
- 【转载】C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte
C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte 转载:http://www.itdos.com/Mvc/20150302/0741255.htm ...
- 【转载】C#将图片转换为二进制流调用
在C#中可以使用MemoryStream类.BinaryFormatter类等来操作图片,将图片读取到二进制数据流中,最终转成二进制数据流进行调用,详细的实现如下方法所示. private byte[ ...
- 使用C#向Sql Sever中存取网络图片和本地图片(二进制流的形式)
先是做普通的,存储我们本地的图片,将它转化为二进制流存储到数据库对应的表中. 代码如下: string path = "../../A.jpg"; FileStream fs = ...
- [转] js实现对图片的二进制流md5计算
//计算图片md5 function img_MD5(img_path,callback) { plus.io.resolveLocalFileSystemURL(img_path, function ...
- Unity C#图片转换二进制流、字符串互转
图片转二进制流转换图片互转 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...
- net9:图片文件转换成二进制流存入SQL数据库,以及从数据库中读取二进制流输出文件
原文发布时间为:2008-08-10 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
随机推荐
- Url Rewrite 重写
前几天看到园子里一篇关于 Url 重写的文章<获取ISAPI_Rewrite重写后的URL>, URL-Rewrite 这项技术早已不是一项新技术了,这个话题也已经被很多人讨论过多次.搜索 ...
- win10中打开SQL Server 2008 的SQL Server配置管理器方法
win10找不到SQL Server配置管理器 搜索 SQLServerManager10.msc,或者运行文件:“C:\Windows\SysWOW64\SQLServerManager10.msc ...
- ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法 update delete get 等方法crud操作
UserAction类代码: package com.swift.action; import com.opensymphony.xwork2.ActionSupport; import com.sw ...
- UIPopoverController
if (popOver == nil) { popOver = [[UIPopoverController alloc] initWithContentViewController:viewVC]; ...
- PostgreSQL学习(1)-- 安装pageinspect extension
1.源码编译 pageinspect的源码在postgre源码包的contrib目录下,解压postgre源码包后进入对应的目录. [root@localhost pageinspect]# pwd ...
- 201621123080 《Java程序设计》 第7周学习总结
1. 本周学习总结 1.1 思维导图:Java图形界面总结 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. 事件 事件源 事件监听器 事件处理方法 1.2 任意 ...
- logging记录了其他操作的问题
做atm作业的时候,记录转账操作的那个功能的文件里,同时也记录了增加账号和冻结账号的操作 2018-11-28 17:14:51,754 : transfer : INFO : 用户edward向用户 ...
- CentOS 7.0 使用 yum 安装 MariaDB 及 简单配置
1.安装MariaDB 安装命令 yum -y install MariaDB-server MariaDB-client 安装完成MariaDB,首先启动MariaDB 设置开机启动 接下来进行Ma ...
- Do not pour out HDU - 5954 数学积分
题目:题目链接 思路:纯高等数学问题,不过不是很好积分,具体积分思路及过程参考大佬博客——https://blog.csdn.net/danliwoo/article/details/53002695 ...
- cento命令之which、whereis、locate、find
[which] 查看可执行文件的位置 语法: [root@localhost ~]# which 可执行文件名称 例如: [root@localhost ~]# which passwd /usr/b ...