項目的預算需要個部分上傳一個word的說明檔案,供線上查看,通過兩天的碰釘子,終於算是完成了,代碼及過程貼出來,請大家指正。
上傳檔案並轉換:
環境:網上下載了FlashPaper,用來轉換文檔成Swf
上傳:
protected void UploadClick(object sender, DirectEventArgs e){string tpl = "Uploaded file: {0}Size: {1} bytes";string gsdm = HttpUtility.UrlDecode(Request.Cookies["danwei"].Value).ToString();string bmdm = HttpUtility.UrlDecode(Request.Cookies["bumen"].Value).ToString();if (this.FileUploadField1.HasFile){string FileExt = this.FileUploadField1.PostedFile.FileName.Substring(this.FileUploadField1.PostedFile.FileName.LastIndexOf("."));//檔案名稱使用單位+部門+年度string FileName = "/BmysDoc/" + gsdm+"."+bmdm+"."+this.kjnd.Text+FileExt;string SwfFile = "/BmysSwf/" + gsdm + "." + bmdm + "." + this.kjnd.Text + ".swf";try{this.FileUploadField1.PostedFile.SaveAs(Server.MapPath(FileName));ConvertToSWF(Server.MapPath(FileName), Server.MapPath(SwfFile));X.Msg.Show(new MessageBoxConfig{Buttons = MessageBox.Button.OK,Icon = MessageBox.Icon.INFO,Title = "Success",Message = string.Format(tpl, this.FileUploadField1.PostedFile.FileName, this.FileUploadField1.PostedFile.ContentLength)});}catch (Exception exc){X.Msg.Show(new MessageBoxConfig{Buttons = MessageBox.Button.OK,Icon = MessageBox.Icon.ERROR,Title = "Fail",Message = exc.ToString()});}}else{X.Msg.Show(new MessageBoxConfig{Buttons = MessageBox.Button.OK,Icon = MessageBox.Icon.ERROR,Title = "Fail",Message = "No file uploaded"});}}
public void ConvertToSWF(string oldFile, string swfFile){System.Diagnostics.Process pc = new System.Diagnostics.Process();pc.StartInfo.FileName = @"F:\flashpaperDemo\FlashPaper2.2\FlashPrinter.exe";//安裝路徑 pc.StartInfo.Arguments = string.Format("{0} -o {1}", oldFile, swfFile);pc.StartInfo.CreateNoWindow = true;pc.StartInfo.UseShellExecute = false;pc.StartInfo.RedirectStandardInput = false;pc.StartInfo.RedirectStandardOutput = false;pc.StartInfo.RedirectStandardError = true;pc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;pc.Start();pc.WaitForExit();pc.Close();pc.Dispose();}
這樣在上傳的同時就處理了轉換
線上查看:
<ext:Button ID="Button4" runat="server" Text="查看文檔" Icon="ArrowRefreshSmall"><DirectEvents><Click OnEvent="ShowSwf"></Click></DirectEvents></ext:Button>
方法:
protected void ShowSwf(object sender, EventArgs e){string gsdm = HttpUtility.UrlDecode(Request.Cookies["danwei"].Value).ToString();string bmdm = HttpUtility.UrlDecode(Request.Cookies["bumen"].Value).ToString();string SwfFile = "/BmysSwf/" + gsdm + "." + bmdm + "." + this.kjnd.Text + ".swf";if (File.Exists(SwfFile) == false){ResourceManager1.AddScript("window.open('ShowSwf.aspx?SwfFile="+SwfFile+"');");}elseX.MessageBox.Alert("提示", "該部門文檔未發現!").Show();}
ShowSwf.aspx代碼:
protected void Page_Load(object sender, EventArgs e){string SwfFile = Request.QueryString["SwfFile"];System.Text.StringBuilder html = new System.Text.StringBuilder();html.Append("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0\" width=\"" + "100%" + "\" height=\"" + "100%" + "\">");html.Append("<param name=\"movie\" value=\"" + SwfFile + "\" />");html.Append("</object>");Response.Write(html.ToString());}
尚未解決的缺點:
1、轉換時視窗會出現轉換過程,是一閃而過,也是覺得不爽,是不是轉到cmd下去處理會好一點沒有測試。
2、沒有百度文庫好看,待努力。
轉自:http://ext.net.cn/thread-1373-1-1.html