在JavaScript中如何解決用execCommand(,javascriptexec

來源:互聯網
上載者:User

在JavaScript中如何解決用execCommand(,javascriptexec

本解決方案僅適應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);  }

以上內容就是本文關於execcommand相容性問題的全部敘述,希望大家喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.