JS中用execCommand("SaveAs")保存頁面兼容性問題解決方案

開發環境:ASP.NET MVC,其他環境僅供參考。

問題描述:在開發中遇到這樣的需求,保存頁面,通常使用JavaScript的saveAs進行保存,各瀏覽器對saveAs支持,見下表。

 

代碼一:初始保存的代碼,只有IE6,7,8支持。
    function CmdSave() {
        var OW = window.open('', "_blank", "");
        var DD = new Date();
        OW.document.open();
        var content = document.getElementById("content").innerHTML;
        OW.document.write(content);
        var name = mineName + "-" + $("#selDate").val() + ".htm";
        OW.document.execCommand("saveAs", false, name);//執行保存,IE6,IE7,IE8有效
        OW.close();
    }

 

解決方案:考慮到下載兼容性好,也能起到保存頁面的作用,故採用了先生成頁面,再下載頁面這樣的解決方案。

代碼二:採用下載方式保存頁面代碼。
    function CmdSave() {
        var css = "<style type='text/css'>.trNormalTd { border-top-width: 0px; border-bottom-width: 0px;text-align:right;}.trLastTd {border-top-width: 0px;text-align:right;}.trFirstTd{border-bottom-width: 0px;text-align: right;}</style>";
        var html = document.getElementById("content").innerHTML;
        var content = css + html;
        var name = mineName + "-" + $("#selDate").val() + ".htm";
        savePage(content, name);
}

    //content 內容 fileName 文件名 先在服務器生成頁面,然後再下載生成的頁面
    function savePage(content, fileName) {
        $.ajax({
            type: 'post',
            dataType: 'text',
            url: 'FXBB/BCYM',
            data: {
                content: content,
                fileName: fileName
            },
            success: function (result) {
                var url = "YXGZ/DBFX/BBCX/FXBB/XZYM?fileName=" + fileName;
                var downloadUrl = window.location.protocol + "//" + window.location.host + "/" + url;
                window.open(downloadUrl);//下載頁面
                //deleteFile(fileName);
            },
            error: function (msg) {
                alert("保存出錯");
            }

        });
    }

		//保存頁面
        public int BCYM(string content, string fileName)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            path = Path.Combine(path, @"Upload\FXBB");
		   //清空保存文件文件夾文件
            foreach (string d in Directory.GetFileSystemEntries(path))
            {
                if (File.Exists(d))
                {
                    File.Delete(d);
                }
            }
		  //生成要保存的頁面
            path = System.AppDomain.CurrentDomain.BaseDirectory;
            path = Path.Combine(path, "Upload/FXBB/" + fileName);

            using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))// File.AppendText(path))
            {
                sw.WriteLine(content);
                sw.Flush();
            }

            return 1;
        }

//下載頁面
     public void XZYM(string fileName)
        {

            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            path = Path.Combine(path, @"Upload\FXBB\" + fileName);
            string filePath = path;//Server.MapPath("DownLoad/aaa.zip");//路徑

            //以字符流的形式下載文件
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";

            //通知瀏覽器下載文件而不是打開
            System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            System.Web.HttpContext.Current.Response.WriteFile(filePath);
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章