跪求—在asp.net 将文本文件转化成二进流,存入数据库的代码! 跪求将文本文件(txt,pdf,word)转化成二进流,存入数据库的代码! 在线等,希望各位朋友们救救急。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //以txt为例byte[] buffer = Encoding.Unicode.GetBytes(""); FileStream fsw = new FileStream( "C:\\A.txt ",FileMode.Create); fsw.Write(buffer,0,buffer.Length); fsw.Close(); //剩下的你懂的 //保存word文档 static void saveword() { FileInfo fi=new FileInfo(@"E:\学习试验项目\WordStore\wordStore\1.doc"); FileStream fs=fi.OpenRead(); byte[] wbytes=new byte[fs.Length]; fs.Read(wbytes,0,Convert.ToInt32(fs.Length)); string sqlstring = "insert into tb_word (memo,document) values ( @memo,@document)"; SqlParameter[] parameters = new SqlParameter[2]; parameters[0]=new SqlParameter("@memo",SqlDbType.NText); parameters[0].Value="ssss";//这里可以将word内容保存到数据库中 parameters[1]=new SqlParameter("@document",SqlDbType.Image); parameters[1].Value = wbytes; DbHelperSQL.ExecuteSql(sqlstring,parameters); Console.WriteLine(i); } //从数据库读取word文档 static void readword() { string sqlstring = "select top 1 * from Tb_word "; DataSet ds= new DataSet(); ds = DbHelperSQL.Query(sqlstring); byte[] File=null; File=(byte[])ds.Tables[0].Rows[0]["document"]; FileStream fs; string filename = System.Environment.GetEnvironmentVariable("Temp").ToString() + @"\temp"; FileInfo fi=new System.IO.FileInfo(filename); fs=fi.OpenWrite(); fs.Write(File,0,File.Length); fs.Close(); string temp = ReadAllFromWord(filename); Console.WriteLine(temp); } #region 读取word /// <summary> /// 读取word所有文字内容(不包含表格) /// </summary> static string ReadAllFromWord(string m_FilePath) { Word.ApplicationClass app = null; Word.Document doc = null; object missing = System.Reflection.Missing.Value; object FileName = m_FilePath;//@"E:\学习试验项目\ReadFromWordDoc\test.doc"; object readOnly = true; object isVisible = false; try { app = new Word.ApplicationClass(); doc = app.Documents.Open(ref FileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); string textString = ""; //读取全部内容 textString = doc.Content.Text.Trim(); // int ParCount = this.getParCount(doc);//段数 // for (int i = 1 ; i <= ParCount ; i++) // { // textString = textString + doc.Paragraphs[i].Range.Text.Trim();//doc.Content.Text.Trim();// // } textString = textString.Replace("\a",""); //替换空串为空。(word中\a代表空串,但在C#中,代表响铃 晕~~)否则显示控制台程序时会响 textString = textString.Replace("\r","\n"); //替换回车为回车换行 return textString; } catch(Exception ex) { throw ex; } finally { if (doc != null) { try { doc.Close(ref missing, ref missing, ref missing); } catch {} doc = null; } if (app != null) { try { app.Quit(ref missing, ref missing, ref missing); } catch {} app = null; } GC.Collect(); GC.WaitForPendingFinalizers(); } } #endregion 所有文件都是以二进制的方式存的BinaryReader:二进制读取类,BinaryWriter :二进制写入类txt File.ReadAllText("") 在一个mvc项目中运行单元测试竟然报错,求助! 请高手指教:特殊字符处理 vs.net2003為什么聯能建web項目 treeview访问权限的问题,烦死人,超级急 各位大侠,进来帮我看看这些函数都应该怎么用啊,谢谢了!!! vs2005中xml的问题 信誉是100的人进来接分。 生成DLL问题 EF 6拉姆达数据访问问题 如何获取客户端显示器的分辨率? 在线等 自定义控件
//以txt为例
byte[] buffer = Encoding.Unicode.GetBytes(""); FileStream fsw = new FileStream( "C:\\A.txt ",FileMode.Create);
fsw.Write(buffer,0,buffer.Length);
fsw.Close(); //剩下的你懂的
static void saveword()
{
FileInfo fi=new FileInfo(@"E:\学习试验项目\WordStore\wordStore\1.doc");
FileStream fs=fi.OpenRead();
byte[] wbytes=new byte[fs.Length];
fs.Read(wbytes,0,Convert.ToInt32(fs.Length));
string sqlstring = "insert into tb_word (memo,document) values ( @memo,@document)"; SqlParameter[] parameters = new SqlParameter[2]; parameters[0]=new SqlParameter("@memo",SqlDbType.NText);
parameters[0].Value="ssss";//这里可以将word内容保存到数据库中
parameters[1]=new SqlParameter("@document",SqlDbType.Image);
parameters[1].Value = wbytes;
DbHelperSQL.ExecuteSql(sqlstring,parameters);
Console.WriteLine(i);
} //从数据库读取word文档
static void readword()
{
string sqlstring = "select top 1 * from Tb_word ";
DataSet ds= new DataSet();
ds = DbHelperSQL.Query(sqlstring);
byte[] File=null;
File=(byte[])ds.Tables[0].Rows[0]["document"];
FileStream fs;
string filename = System.Environment.GetEnvironmentVariable("Temp").ToString() + @"\temp";
FileInfo fi=new System.IO.FileInfo(filename);
fs=fi.OpenWrite();
fs.Write(File,0,File.Length);
fs.Close();
string temp = ReadAllFromWord(filename);
Console.WriteLine(temp);
} #region 读取word
/// <summary>
/// 读取word所有文字内容(不包含表格)
/// </summary>
static string ReadAllFromWord(string m_FilePath)
{
Word.ApplicationClass app = null;
Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object FileName = m_FilePath;//@"E:\学习试验项目\ReadFromWordDoc\test.doc";
object readOnly = true;
object isVisible = false;
try
{
app = new Word.ApplicationClass();
doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref isVisible, ref missing,
ref missing, ref missing, ref missing); string textString = "";
//读取全部内容
textString = doc.Content.Text.Trim();
// int ParCount = this.getParCount(doc);//段数
// for (int i = 1 ; i <= ParCount ; i++)
// {
// textString = textString + doc.Paragraphs[i].Range.Text.Trim();//doc.Content.Text.Trim();//
// }
textString = textString.Replace("\a",""); //替换空串为空。(word中\a代表空串,但在C#中,代表响铃 晕~~)否则显示控制台程序时会响
textString = textString.Replace("\r","\n"); //替换回车为回车换行
return textString;
}
catch(Exception ex)
{
throw ex;
}
finally
{
if (doc != null)
{
try
{
doc.Close(ref missing, ref missing, ref missing);
}
catch
{}
doc = null;
}
if (app != null)
{
try
{
app.Quit(ref missing, ref missing, ref missing);
}
catch
{}
app = null;
}
GC.Collect();
GC.WaitForPendingFinalizers(); }
}
#endregion
BinaryReader:二进制读取类,
BinaryWriter :二进制写入类
txt File.ReadAllText("")