c#上傳文件並將word pdf轉化成txt存儲並將內容寫入數據庫
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 Microsoft.Office.Core;//添加引用 com Microsoft.Office 12.0 using Microsoft.Office.Interop.PowerPoint;//添加引用 com Microsoft Powerpoint 12.0 using Microsoft.Office.Interop.Word; //添加引用 com Microsoft word 12.0 using System.IO; using org.pdfbox.util;//網上下載PDFBox-0.7.3 引用PDFBox-0.7.3.dll、IKVM.GNU.Classpath.dll、IKVM.Runtime.dll、FontBox-0.1.0-dev.dll using org.pdfbox.pdmodel; using System.Text; public partial class _Default : System.Web.UI.Page { dbcommand db = new dbcommand(); prompt pt = new prompt(); protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) return; } protected void decide_Click(object sender, EventArgs e) { //獲取文件全名 string filename = fj.PostedFile.FileName.ToString(); //上傳文件類型 string filetype = filename.Substring(filename.LastIndexOf('.') + 1); if (filetype == "doc" || filetype == "docx" || filetype == "pdf") { string strpath = "~/Upload/" + filename; fj.PostedFile.SaveAs(Server.MapPath(strpath)); //獲取文件名 string strname = filename.Substring(0, filename.LastIndexOf('.')); FileInfo fi1 = new FileInfo(@"E:\FUL\Upload\" + filename); FileInfo fi2 = new FileInfo(@"E:\FUL\txt\" + strname + ".txt"); if (filetype == "pdf") pdf2txt(fi1, fi2); else word2text(fi1, fi2); StreamReader sr=text2reader(fi2); string sql="insert into fujian(subject,content) values('"+subject.Text+"','"+sr.ReadToEnd()+"')"; int num=db.AffectedRow(sql);//寫入數據庫,返回影響行數函數,這里你可以自己寫 if (num > 0) pt.message("上傳成功!");//彈出對話框,這里你可以自己寫 } } /// <summary> /// pdf文件轉成txt /// </summary> /// <param name="file"></param> /// <param name="txtfile"></param> public void pdf2txt(FileInfo file, FileInfo txtfile) { PDDocument doc = PDDocument.load(file.FullName); PDFTextStripper pdfStripper = new PDFTextStripper(); string text = pdfStripper.getText(doc); StreamWriter swPdfChange = new StreamWriter(txtfile.FullName, false, Encoding.GetEncoding("gb2312")); swPdfChange.Write(text); swPdfChange.Close(); } /// <summary> ///word文檔轉成txt,對於doc文件中的表格,讀出的結果是去除掉了網格線,內容按行讀取。 /// </summary> /// <param name="file"></param> /// <param name="txtfile"></param> public void word2text(FileInfo file, FileInfo txtfile) { object readOnly = true; object missing = System.Reflection.Missing.Value; object fileName = file.FullName; Microsoft.Office.Interop.Word.ApplicationClass wordapp = new Microsoft.Office.Interop.Word.ApplicationClass(); Document doc = wordapp.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 missing, ref missing, ref missing, ref missing, ref missing); string text = doc.Content.Text; doc.Close(ref missing, ref missing, ref missing); wordapp.Quit(ref missing, ref missing, ref missing); StreamWriter swWordChange = new StreamWriter(txtfile.FullName, false, Encoding.GetEncoding("gb2312")); swWordChange.Write(text); swWordChange.Close(); } public void ppt2txt(FileInfo file, FileInfo txtfile) { Microsoft.Office.Interop.PowerPoint.Application pa = new Microsoft.Office.Interop.PowerPoint.ApplicationClass(); Microsoft.Office.Interop.PowerPoint.Presentation pp = pa.Presentations.Open(file.FullName, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse); string pps = ""; StreamWriter swPPtChange = new StreamWriter(txtfile.FullName, false, Encoding.GetEncoding("gb2312")); foreach (Microsoft.Office.Interop.PowerPoint.Slide slide in pp.Slides) { foreach (Microsoft.Office.Interop.PowerPoint.Shape shape in slide.Shapes) pps += shape.TextFrame.TextRange.Text.ToString(); } swPPtChange.Write(pps); swPPtChange.Close(); } /// <summary> /// 取txt文件的內容 /// </summary> /// <param name="file"></param> /// <returns></returns> public StreamReader text2reader(FileInfo file) { StreamReader st = null; switch (file.Extension.ToLower()) { case ".txt": st = new StreamReader(file.FullName, Encoding.GetEncoding("gb2312")); break; case ".doc": FileInfo wordfile = new FileInfo(@"E:\myprograms\200807program\FileSearch\App_Data\word2txt.txt");//不能使用相對路徑,想辦法改進 word2text(file, wordfile); st = new StreamReader(wordfile.FullName, Encoding.GetEncoding("gb2312")); break; case ".pdf": FileInfo pdffile = new FileInfo(@"E:\myprograms\200807program\FileSearch\App_Data\pdf2txt.txt"); pdf2txt(file, pdffile); st = new StreamReader(pdffile.FullName, Encoding.GetEncoding("gb2312")); break; case ".ppt": FileInfo pptfile = new FileInfo(@"E:\myprograms\200807program\FileSearch\App_Data\ppt2txt.txt"); ppt2txt(file, pptfile); st = new StreamReader(pptfile.FullName, Encoding.GetEncoding("gb2312")); break; } st = new StreamReader(file.FullName, Encoding.GetEncoding("gb2312")); return st; } }
//下面的和上面的沒有直接關系,下面的只是上傳文件
上傳附件
<asp:FileUpload ID="FileUpload1" runat="server" />
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Web.UI.WebControls.WebParts; 9 using System.Web.UI.HtmlControls; 10 public partial class _Default : System.Web.UI.Page 11 { 12 protected void Page_Load(object sender, EventArgs e) 13 { 14 } 15 protected void Button1_Click(object sender, EventArgs e) 16 { 17 string name = FileUpload1.FileName;//上傳文件名 18 string size = FileUpload1.PostedFile.ContentLength.ToString();//得到上傳文件的大小 19 string type = FileUpload1.PostedFile.ContentType;//得上傳文件的類型 20 string type2 = name.Substring(name.LastIndexOf(".")+1);//得后綴名 21 string ipath = Server.MapPath("upimg") + "\\" + name; 22 if (FileType == "pdf" || FileType == "doc" || FileType == "xls" || FileType == "txt" || FileType == "jpg" || FileType == "JPG" || FileType == "gif" || FileType == "bmp" || FileType == "rar" || FileType == "zip" || FileType == "dwg") 23 { 24 FileUpload1.SaveAs("ipath"); 25 Image1.ImageUrl = wpath; 26 Label1.Text = "上傳文件的名稱" + name + "上傳文件的大小" + size + "上傳文件的類型" + type + "后綴名" + type2 + "實際路徑" + ipath; 27 } 28 } 29 }
打開附件:
前台:<asp:HyperLink ID="fj" runat="server" Visible="False"></asp:HyperLink>
1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Collections; 5 using System.Web; 6 using System.Web.Security; 7 using System.Web.UI; 8 using System.Web.UI.WebControls; 9 using System.Web.UI.WebControls.WebParts; 10 using System.Web.UI.HtmlControls; 11 public partial class ViewBlog : System.Web.UI.Page 12 { 13 dbcommand db = new dbcommand(); 14 prompt pt = new prompt(); 15 protected void Page_Load(object sender, EventArgs e) 16 { 17 if (Session["username"] == null) 18 { 19 Response.Write("<script type='text/javascript'>alert('" + "請先登錄!" + "');top.location='Login.aspx';</script>"); 20 Response.End(); 21 } 22 int id = int.Parse(HttpContext.Current.Request.QueryString["id"].ToString()); 23 string sql = "select T_USER.username,T_Blog.* from T_USER,T_Blog where T_USER.id=T_Blog.blogUser and T_Blog.id=" + id; 24 DataSet ds = db.GetDateset(sql); 25 以下是顯示附件信息 26 if (ds.Tables[0].Rows[0]["FileName"].ToString() != "")//如果有附件 27 { 28 fj.Text = ds.Tables[0].Rows[0]["FileName"].ToString();//顯示附件名稱 29 fj.Visible = true; 30 fj.NavigateUrl = "Upload/" + ds.Tables[0].Rows[0]["FileName"].ToString();//附件所在路徑 31 fj.Target = "_blank"; 32 } 33 } 34 }
FileUpload默認最大上傳4m 要上傳更大 可是web.config中設置 代碼如下:
1 <?xml version="1.0"?> 2 <!-- 3 注意: 除了手動編輯此文件以外,您還可以使用 4 Web 管理工具來配置應用程序的設置。可以使用 Visual Studio 中的 5 “網站”->“Asp.Net 配置”選項。 6 設置和注釋的完整列表在 7 machine.config.comments 中,該文件通常位於 8 \Windows\Microsoft.Net\Framework\v2.x\Config 中 9 --> 10 <configuration> 11 <appSettings/> 12 <connectionStrings> 13 <!--連接數據庫 這里是sql server2008--> 14 <add name="strcnn" connectionString="Data Source=.;Initial Catalog=inshineProject;Persist Security Info=True;User ID=sa;Password=sa;Pooling=False" providerName="System.Data.SqlClient"/> </connectionStrings> 15 <system.web> 16 <!-- 17 設置 compilation debug="true" 將調試符號插入 18 已編譯的頁面中。但由於這會 19 影響性能,因此只在開發過程中將此值 20 設置為 true。 21 --> 22 <!--這里調車最多可上傳200M ,可以根據自己的需要設置更大--> 23 <httpRuntime executionTimeout="10000 " maxRequestLength="2048000 "/> 24 <compilation debug="true"> 25 <assemblies> 26 <add assembly="System.Design, Version=2.0.0.0, Culture=neutral,PublicKeyToken=B03F5F7F11D50A3A"/> 27 <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation> 28 <!-- 29 通過 <authentication> 節可以配置 ASP.NET 使用的 30 安全身份驗證模式, 31 以標識傳入的用戶。 32 --> 33 <authentication mode="Windows"/> 34 <!-- 35 如果在執行請求的過程中出現未處理的錯誤, 36 則通過 <customErrors> 節可以配置相應的處理步驟。具體說來, 37 開發人員通過該節可以配置 38 要顯示的 html 錯誤頁 39 以代替錯誤堆棧跟蹤。 40 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> 41 <error statusCode="404" redirect="FileNotFound.htm" /> 42 </customErrors> 43 --> 44 </system.web> 45 </configuration>