c# 使用base64在数据库中存取文件
2007-05-04 11:13
447 查看
对应的数据表的字段类型是:text
保存的时候:
string strContent = XmlLib.XML_TextToDoc(TextBox_Content.Text);
//toBase64
strContent = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(strContent));
再插入到数据表中.
打开的时候
string strSQL = "Select * From sys_DocManage Where [ID]="+s_strID;
string strConn = Session["ConnString"].ToString();
OleDbConnection objConn = new OleDbConnection(strConn);
objConn.Open();
OleDbCommand objComm = new OleDbCommand(strSQL, objConn);
OleDbDataReader objReader = objComm.ExecuteReader();
if(objReader.Read())
{
string strContent = objReader["Content"].ToString();
//cover base64 to string
if (strContent != "")
{
//转成文本便于编辑
byte[] ayByte = Convert.FromBase64String(strContent);
strContent = System.Text.Encoding.Default.GetString(ayByte);
}
TextBox_Content.Text = strContent;
txtFieldName.Text = objReader["Name"].ToString();
}
objReader.Close();
objConn.Close();
下载的时候
string id = Request["id"];
if (id != null && id != string.Empty)
{
string pattern = @"^[1-9]/d*$";
Regex reg = new Regex(pattern);
if (reg.IsMatch(id))
{
string connStr = Session["ConnString"].ToString();
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open();
//
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT Content,Name,FileType FROM sys_DocManage WHERE ID=" + id;
OleDbDataReader dr = cmd.ExecuteReader();
dr.Read();
int fileDataCol = 0;
string filename = dr["Name"].ToString();
string fileType = dr["FileType"].ToString();
string fileContent = dr["Content"].ToString();
byte[] a = Convert.FromBase64String(fileContent);//base64 的下载
//Byte[] a = System.Text.Encoding.Default.GetBytes(fileContent);//非base64 的下载
Response.ContentType = "Application/msword";
//你把这一句换成相应类型即可
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + "." + fileType );
this.Response.Clear();
System.IO.Stream fs = this.Response.OutputStream;
fs.Write(a, 0, a.Length);
fs.Close();
this.Response.End();
}
else
{
Response.Write("Error: 参数错误");
}
}
else
{
Response.Write("Error: 参数错误");
}
保存的时候:
string strContent = XmlLib.XML_TextToDoc(TextBox_Content.Text);
//toBase64
strContent = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(strContent));
再插入到数据表中.
打开的时候
string strSQL = "Select * From sys_DocManage Where [ID]="+s_strID;
string strConn = Session["ConnString"].ToString();
OleDbConnection objConn = new OleDbConnection(strConn);
objConn.Open();
OleDbCommand objComm = new OleDbCommand(strSQL, objConn);
OleDbDataReader objReader = objComm.ExecuteReader();
if(objReader.Read())
{
string strContent = objReader["Content"].ToString();
//cover base64 to string
if (strContent != "")
{
//转成文本便于编辑
byte[] ayByte = Convert.FromBase64String(strContent);
strContent = System.Text.Encoding.Default.GetString(ayByte);
}
TextBox_Content.Text = strContent;
txtFieldName.Text = objReader["Name"].ToString();
}
objReader.Close();
objConn.Close();
下载的时候
string id = Request["id"];
if (id != null && id != string.Empty)
{
string pattern = @"^[1-9]/d*$";
Regex reg = new Regex(pattern);
if (reg.IsMatch(id))
{
string connStr = Session["ConnString"].ToString();
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open();
//
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT Content,Name,FileType FROM sys_DocManage WHERE ID=" + id;
OleDbDataReader dr = cmd.ExecuteReader();
dr.Read();
int fileDataCol = 0;
string filename = dr["Name"].ToString();
string fileType = dr["FileType"].ToString();
string fileContent = dr["Content"].ToString();
byte[] a = Convert.FromBase64String(fileContent);//base64 的下载
//Byte[] a = System.Text.Encoding.Default.GetBytes(fileContent);//非base64 的下载
Response.ContentType = "Application/msword";
//你把这一句换成相应类型即可
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + "." + fileType );
this.Response.Clear();
System.IO.Stream fs = this.Response.OutputStream;
fs.Write(a, 0, a.Length);
fs.Close();
this.Response.End();
}
else
{
Response.Write("Error: 参数错误");
}
}
else
{
Response.Write("Error: 参数错误");
}
相关文章推荐
- 使用ADO实现vc中二进制文件数据的存取(以图像在数据库中的存取为例)
- C#使用配置文件连接数据库未实例化问题
- 复习课程jdbc:使用配置文件properties进行连接数据库,数据库存取图片,批处理,时间戳,事物回滚等等
- 使用C#进行图片的数据库存取
- C#操作数据库中存取图片文件
- C#中读取自定义的config文件(数据库的连接符大多情况使用)
- 使用ADO GetChunk/AppendChunk 数据库存取二进制文件(如程序,图象等)
- 使用ADO实现vc中二进制文件数据的存取(以图像在数据库中的存取为例)
- 使用C#创建Access2010或Access2007 accdb 数据库文件
- 使用C#将二进制文件保存到数据库中
- 本文使用c#通过oledb可获取excel文件表的结构信息,同理也可以获得access等数据库的表结构信息,实现如下:
- C#使用单文件数据库的问题
- C#使用单文件数据库的问题
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- C#操作数据库中存取图片文件的方法
- C#连接数据库_使用读取配置文件的方式
- C# WinForm程序App.Config数据库连接配置文件的使用过程
- 使用C#在數據庫中存取文件
- Using Settings in C# 在C#中使用APP.CONFIG配置文件
- MySQL如何存储图片?---一个向数据库存取image文件的jsp程序