有過ftp下載檔案經驗的人都有體驗,單獨下載一個100M的檔案比分開下載100個1M的檔案速度快很多,這是因為下載需要解析網域名稱,建立串連等額外開銷的時間,因此下載100個檔案就要做100次這種額外的開銷。因此如果把這個100個檔案整合成一個檔案下載速度會快很多。基於上述的方法,可以對HTML中的JS檔案也做類似的處理,無論js檔案多少個,通過設定檔,把N個js檔案合并到一個檔案下載。實現手段通過httpHandler。具體代碼如下:web.config檔案中: <httpHandlers> <add verb="*" path="MergedJS.js" type="MergeJS.GetJS"/> </httpHandlers> 只對MergedJS.js的請求檔案作合并,具體請求到哪個檔案,有querystring參數給定,比如本例中使用MergedJS.js?jsid=myjs這種方式。對於其他類型的js檔案,則按照IIS預設的請求方式處理。 <appSettings> <add key="myjs" value="jquery-1.4.1-vsdoc.js, jquery-ui-1.8.16.custom.min.js, jquery-1.4.1.js, jquery-1.7.min.js, jquery-1.5.2.min.js, jquery-1.4.1.min.js, jquery.maskedinput-1.3.js, json2.min.js, jquery.hotkeys-0.0.3.js, jquery.loadmask.min.js, My.js"/> </appSettings> 上述配置,主要用來指定myjs使用哪些檔案來合并。MergeJS.GetJS 這個HttpHandler主要用來處理js。此處涉及到一個緩衝的問題,通常情況下,js檔案會預設緩衝在本地瀏覽器緩衝中,但是對於MergedJS.js這種通過httpHandler所處理的,被視為動態內容,並不會緩衝在瀏覽器中,而是每次都會從伺服器端擷取最新的內容。因此可以通過編碼,強制使用瀏覽器緩衝。由於反覆讀取js所做IO操作也是效能瓶頸,因此在global中首先將所有的js預讀存在dictionary中,用的時候直接從dictionary中輸出,避免了IO操作。代碼能夠判斷js檔案是否做了修改,如果修改了,dictionary中的值也會得到更新。Handler處理部分的代碼如下: /// <summary> /// GetJS 的摘要說明 /// </summary> public class GetJS : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/x-javascript";//表示是javascript檔案類型 string jsid = context.Request.Params["jsid"].ToString(); //擷取所有需要Merge的檔案 string[] jsfiles = System.Configuration.ConfigurationManager.AppSettings[jsid].ToString().Replace("\r", "").Replace("\n", "").Split(','); //記錄檔案的最後修改時間,取最新修改的那個檔案的時間。 DateTime lastChangeTime = new DateTime(); StringBuilder sb = new StringBuilder(); foreach (var js in jsfiles) { if (Global.dic_jsfiles.ContainsKey(js)) { DateTime tmplastDateTime = new FileInfo(Global.dic_jsfiles[js].filefullName).LastWriteTime; tmplastDateTime = DateTime.Parse(tmplastDateTime.ToString("yyyy-MM-dd HH:mm:ss"));//去除毫秒資訊。 if (tmplastDateTime > Global.dic_jsfiles[js].lastModifiedTime)//如果最後修改時間更改,更新dic_jsfiles內容 { Global.dic_jsfiles[js].content = Common.ReadFile(Global.dic_jsfiles[js].filefullName);//讀取新的檔案 Global.dic_jsfiles[js].lastModifiedTime = tmplastDateTime; } } else//如果不存在,則讀取該檔案 { string filename = context.Request.PhysicalApplicationPath + "Scripts\\" + js.Trim(); Global.dic_jsfiles.Add(filename,new jsFileInfo() { filefullName = filename,content=Common.ReadFile(filename),lastModifiedTime=new FileInfo(filename).LastWriteTime }); Common.ReadFile(Global.dic_jsfiles[js].filefullName); } sb.Append(Global.dic_jsfiles[js].content + ";"); } //檔案最後修改時間 lastChangeTime = Global.dic_jsfiles.Max(m => m.Value.lastModifiedTime); DateTime If_Modified_Since = new DateTime(); if (context.Request.Headers["If-Modified-Since"] == null || context.Request.Headers["If-Modified-Since"] == "") www.2cto.com { If_Modified_Since = new DateTime(1900, 1, 1);//如果讀取的要求標頭中If-Modified-Since沒有值,就給它一個預設值為19000101 } else { If_Modified_Since = DateTime.Parse(context.Request.Headers["If-Modified-Since"]); } if (If_Modified_Since >= lastChangeTime)//如果用戶端要求標頭中的時間最後一次修改時間,比真實的最後修改時間還新,則直接返回304代碼 { context.Response.StatusCode = 304;//返回304代碼,使其讀取緩衝 context.Response.End(); } else//否則 { //顯示內容 context.Response.AddHeader("last-modified", lastChangeTime.ToString()); } context.Response.Write(sb.ToString()); } private string ReadFile(string filename) { using (StreamReader sr = new StreamReader(filename, System.Text.UTF8Encoding.UTF8)) { return sr.ReadToEnd(); } } public bool IsReusable { get { return false; } } } 效果如下:使用組合下載的情況,js的下載耗費時間73ms。但是分開下載的話就比較耗時。當然,並不是合并為一個檔案,下載的速度就一定快,具體的下載速度和策略還受到瀏覽器的影響,有的瀏覽器同時開的請求線程多,也會影響下載速度。但是總體來說,如果檔案數多的話,下載單一檔案總會快點的。