代码
#region 数据库表中Image类型(二进制)写入文本文件
protected void Button1_Click(object sender, EventArgs e)
{
string arg_msg = "";
string str = "";
string ls_sql = "select * from r3_oa_image where billid=2";
DataTable dt = cs_con.Select(ls_sql, null, ref arg_msg).Tables[0];
if (dt.Rows.Count > 0)
{
string add_file = "";
string file_name = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
byte[] image = (byte[])dt.Rows[i]["add_file"];//其中add_file字段是Image类型
StringBuilder lkj = new StringBuilder();
for (int k = 0; k < image.Length; k++)
{
lkj.Append("," + image[k].ToString().Trim());
}
//导入到文本文件
string filename = @"d:\test.txt";
FileInfo fileinfo = new FileInfo(filename);
using (FileStream fs = fileinfo.OpenWrite())
{
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.Write(lkj);
sw.Flush();
sw.Close();
}
}
}
}
#endregion
代码
#region 文本文件转换成二进制存入数据库
protected void Button2_Click(object sender, EventArgs e)
{
string filename = @"d:\test.txt";
StreamReader sr = new StreamReader(filename, System.Text.Encoding.Default);
String lkj = sr.ReadToEnd();
sr.Close();
string[] strs = lkj.ToString().Trim(',').Split(',');
byte[] textByte = new byte[35811];
for (int j = 0; j < 951296; j++)//这里的951296是前面方法中image.length,这里有点BUG获取不到大小,使用byte数组定义大小,
{
textByte[j] = Convert.ToByte(strs[j]);
}
SqlConnection con = cs_con.DBconn();
SqlCommand command = new SqlCommand();
command.Connection = con;
command.CommandText = "insert into r3_oa_image(billid,add_file,upload_time,file_name,remark) values(1,@add_file,getdate(),'npActiveXFirefox4x.xpi','打印控件');";
command.Parameters.Add("@add_file", SqlDbType.Image, textByte.Length, "add_file");
command.Parameters["@add_file"].Value = textByte;
con.Open();
command.ExecuteNonQuery();
con.Close();
}
#endregion
#region 文本文件转换成二进制存入数据库
protected void Button2_Click(object sender, EventArgs e)
{
string filename = @"d:\test.txt";
StreamReader sr = new StreamReader(filename, System.Text.Encoding.Default);
String lkj = sr.ReadToEnd();
sr.Close();
string[] strs = lkj.ToString().Trim(',').Split(',');
byte[] textByte = new byte[35811];
for (int j = 0; j < 951296; j++)//这里的951296是前面方法中image.length,这里有点BUG获取不到大小,使用byte数组定义大小,
{
textByte[j] = Convert.ToByte(strs[j]);
}
SqlConnection con = cs_con.DBconn();
SqlCommand command = new SqlCommand();
command.Connection = con;
command.CommandText = "insert into r3_oa_image(billid,add_file,upload_time,file_name,remark) values(1,@add_file,getdate(),'npActiveXFirefox4x.xpi','打印控件');";
command.Parameters.Add("@add_file", SqlDbType.Image, textByte.Length, "add_file");
command.Parameters["@add_file"].Value = textByte;
con.Open();
command.ExecuteNonQuery();
con.Close();
}
#endregion
下载:
代码
protected void Button3_Click(object sender, EventArgs e)
{
string arg_msg = "";
if (!file_down("1", ref arg_msg))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "js", "alert('" + arg_msg.ToString().Trim() + "');", true);
}
}
#region 文件下载
private bool file_down(string bill, ref string arg_msg)
{
bool rslt = true;
try
{
if (bill == null && bill.Trim().Length > 0)
{
rslt = false;
arg_msg = "下载失败";
goto ext_file_down;
}
Byte[] bytes;
string ls_down_file = "select * from r3_oa_image WHERE billid=" + bill;
DataTable dt = cs_con.Select(ls_down_file, null, ref arg_msg).Tables[0];
bytes = (Byte[])(dt.Rows[0]["add_file"]);
string fileName = dt.Rows[0]["file_name"].ToString().Trim();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream"; // 设置输出流的Http MIME类型//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); //fileName为需要下载的文件名
Response.BinaryWrite(bytes); // 写入输入流
Response.Flush(); // 向客户端发送数据流
Response.End();
}
catch (Exception ex_file_down)
{
rslt = false;
arg_msg = "下载失败:" + ex_file_down.Message;
goto ext_file_down;
}
ext_file_down: ;
return rslt;
}
#endregion
{
string arg_msg = "";
if (!file_down("1", ref arg_msg))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "js", "alert('" + arg_msg.ToString().Trim() + "');", true);
}
}
#region 文件下载
private bool file_down(string bill, ref string arg_msg)
{
bool rslt = true;
try
{
if (bill == null && bill.Trim().Length > 0)
{
rslt = false;
arg_msg = "下载失败";
goto ext_file_down;
}
Byte[] bytes;
string ls_down_file = "select * from r3_oa_image WHERE billid=" + bill;
DataTable dt = cs_con.Select(ls_down_file, null, ref arg_msg).Tables[0];
bytes = (Byte[])(dt.Rows[0]["add_file"]);
string fileName = dt.Rows[0]["file_name"].ToString().Trim();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream"; // 设置输出流的Http MIME类型//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); //fileName为需要下载的文件名
Response.BinaryWrite(bytes); // 写入输入流
Response.Flush(); // 向客户端发送数据流
Response.End();
}
catch (Exception ex_file_down)
{
rslt = false;
arg_msg = "下载失败:" + ex_file_down.Message;
goto ext_file_down;
}
ext_file_down: ;
return rslt;
}
#endregion
数据库表中IMAGE类型字段的内容写入到txt文档(即文本文件)Button1_Click
用户也可以从写入的文本文件通过Button2_Click的方法写入数据库
用户功能下载:Button3_Click的内容
主要是如下一个 Setup_online.msi上传到数据库,保存至add_file字段
通过button1的方法把add_file字段的内容写入test.txt文本文件,并保存,
如果某个程序需要Setup_online.msi的内容,即只要拷贝test.txt文件,通过button2即可,上传至数据库
然后通过下载功能既可以得到Setup_online.msi的文件