這裏只作一個簡單的實例html
public ActionResult Index() { string path = Server.MapPath("/test/");//文件輸出目錄 Encoding code = Encoding.GetEncoding("gb2312"); StreamWriter sw = null; string str = "<html><body>$ShowArticle$</body></html>";//讀取模版頁面html代碼 string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";//靜態文件名 // 替換內容 str = str.Replace("$ShowArticle$", "你好"); // 寫文件 try { sw = new StreamWriter(Path.Combine(path, htmlfilename), true, code); sw.Write(str); sw.Close(); byte[] buffer = null; var ms = new MemoryStream(); using (var file = ZipFile.Create(ms)) { file.BeginUpdate(); foreach (string item in Directory.GetFiles(Server.MapPath("/test"))) { file.Add(Server.MapPath(Path.Combine( "test",Path.GetFileName(item)))); } file.CommitUpdate(); buffer = new byte[ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); } //IE和其它瀏覽器都要進行UTF-8編碼,中文不編碼會出現亂碼 Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode("test") + ".zip"); Response.BinaryWrite(buffer); Response.Flush(); Response.End(); } catch (Exception ex) { return Content(ex.Message); } finally { sw.Close(); } return View(); }