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 ...
随机推荐
- java用org.apache.poi包操作excel
一.POI简介 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API 目前比较成熟的是HSSF接口,处理MS Excel(97- ...
- ReactiveCocoa入门-part2
ReactiveCocoa是一个框架,它能让你在iOS应用中使用函数响应式编程(FRP)技术.在本系列教程的第一部分中,你学到了如何将标准的动作与事件处理逻辑替换为发送事件流的信号.你还学到了如何转换 ...
- A. Vitya in the Countryside
A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...
- 【树形dp】bzoj4726: [POI2017]Sabota?
找点概率期望的题做一做 Description 某个公司有n个人, 上下级关系构成了一个有根树.其中有个人是叛徒(这个人不知道是谁).对于一个人, 如果他 下属(直接或者间接, 不包括他自己)中叛徒占 ...
- JQuery图片轮播实例
HTML+CSS代码: <!doctype html> <html> <head> <meta charset="utf-8"> & ...
- Mysql中反引号和单引号的区别
反引号,一般在ESC键的下方. 它是为了区分MYSQL的保留字与普通字符而引入的符号.举个例子:SELECT `select` FROM `test` WHERE select='字段值'在test表 ...
- mysql中的的按小数位截取
format()函数返回类型是字符串,满三位会加一个逗号. 针对数字类型转换建议使用 convert或者cast函数,用法如下: format(param, 2) (不建议) convert(para ...
- 【Shiro】调用doGetAuthenticationInfo进行认证成功之后,isAuthenticated是false的问题。
使用@Configuration配置shiro无状态登录时出现的问题,在subject.login之后当前线程重新绑定了一个假定subject,isAuthenticated. 这里自定义的访问拦截器 ...
- MySQL查询时,查询结果如何按照where in数组排序
MySQL查询时,查询结果如何按照where in数组排序 在查询中,MySQL默认是order by id asc排序的,但有时候需要按照where in 的数组顺序排序,比如where in的id ...
- Cplex: MIP Control Callback
*本文主要记录和分享学习到的知识,算不上原创 *参考文献见链接 之前,我们有简单提到Cplex中的MIP Callback Interface,包括了Informational callback, q ...