2011年5月17日 星期二

讓 fMath 的設定檔改抓 web.config

舉例來說,數學符號產生工具 fMath,他將可以設定的參數都放在 configMathMLEditor.xml 裡,由上圖可知,fMath 在執行時,會去把 XML 檔案讀進來。這的確是個很方便的維護方法,可是在 Asp.Net 裡自己已經有一個 Web.Config 了,是否可以將 configMathMLEditor.xml  裡某個屬性「urlGenerateImage」改成去讀取 Web.Config 的值呢?

進一步去看 fMath 的使用方式如下:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="850" height="460" id="editML" name="editML" align="middle">
 <param name=wmode value="transparent">
 <param name="allowScriptAccess" value="sameDomain"/>
 <param name="allowFullScreen" value="true"/>
 <param name="loop" value="false"/>
 <param name="quality" value="high" />
 <param name="flashVars" value="configUrl=configMathMLEditor.xml"/>
 <param name="movie" value="mathml/MathMLEditor.swf?configUrl=configMathMLEditor.xml" />
 <embed src="mathml/MathMLEditor.swf?configUrl=configMathMLEditor.xml"
     wmode="transparent"
  flashVars="configUrl=configMathMLEditor.xml"
  loop="false"
  quality="high"
  width="850"
  height="460"
        id="editML"
  name="editML"
  align="middle"
        swliveconnect="true"
  allowScriptAccess="sameDomain"
  allowFullScreen="true"
  type="application/x-shockwave-flash"
  pluginspage="http://www.adobe.com/go/getflashplayer" />
</object>

裡面有提到使用 configMathMLEditor.xml 的地方一共有四個。而我的作法,則是另外寫一隻新的 config.ashx,讓它先去讀取 configMathMLEditor.xml 的原始資料,而這個 xml 裡,已經先將 urlGenerateImage 的值用 $Capture 來取代了。接著再使用取代變數的方式,將 $Capture 以 Web.Config 裡 appSettings 的值來替換。如此,上面提到有4個地方引用了 configMathMLEditor.xml ,就要改成 config.ashx 了,也就是如下:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="850" height="460" id="editML" name="editML" align="middle">
 <param name=wmode value="transparent">
 <param name="allowScriptAccess" value="sameDomain"/>
 <param name="allowFullScreen" value="true"/>
 <param name="loop" value="false"/>
 <param name="quality" value="high" />
 <param name="flashVars" value="configUrl=config.ashx"/>
 <param name="movie" value="mathml/MathMLEditor.swf?configUrl=config.ashx" />
 <embed src="mathml/MathMLEditor.swf?configUrl=config.ashx"
     wmode="transparent"
  flashVars="configUrl=config.ashx"
  loop="false"
  quality="high"
  width="850"
  height="460"
        id="editML"
  name="editML"
  align="middle"
        swliveconnect="true"
  allowScriptAccess="sameDomain"
  allowFullScreen="true"
  type="application/x-shockwave-flash"
  pluginspage="http://www.adobe.com/go/getflashplayer" />
</object>

最後的重點,就是要撰寫自己的 config.ashx ,簡單的測試程式如下:

<%@ WebHandler Language="C#" Class="config" %>

using System;
using System.Web;
using System.IO;

public class config : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        
        context.Response.ContentType = "text/xml";
        
        Stream iStream = null;
        
        // Identify the file to download including its path.
        string filepath =Path.Combine(context.Server.MapPath("./"), "configMathMLEditor.xml");

        // Identify the file name.
        string filename = System.IO.Path.GetFileName(filepath);

     
        // Open the file.
        iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
                    System.IO.FileAccess.Read, System.IO.FileShare.Read);

        //Read Data from FileStream and save to string
        StreamReader sRead = new StreamReader(iStream);
        string strNewFile = sRead.ReadToEnd();
        sRead.Close();

        //將變數取代為 web.config  的值
        strNewFile = strNewFile.Replace("$Capture", System.Configuration.ConfigurationManager.AppSettings["CaptureUrl"]);

        context.Response.Write(strNewFile);
        context.Response.Flush();
        context.Response.End();
      
        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

經過一番打造之後,就可以讓 fMath 裡的設定值,改成去抓 web.Config 了。

參考:
01:response.WriteFile 無法下載大型檔案
02:http://www.fmath.info/
03:FileStream - editing an existing text file

沒有留言:

張貼留言