欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在JavaScript中如何解決用execCommand(

 更新時(shí)間:2015年10月19日 09:18:08   作者:cxiaoluab  
這篇文章主要給大家介紹在JavaScript中如何解決用execCommand("SaveAS")保存頁(yè)面兼容性問(wèn)題,設(shè)計(jì)到execCommand兼容性問(wèn)題,感興趣的朋友可以參考下本篇文章

本解決方案僅適應(yīng)asp.net mvc 開(kāi)發(fā)環(huán)境,其他環(huán)境僅供參考。

問(wèn)題描述:在開(kāi)發(fā)中遇到這樣的需求,保存頁(yè)面,通常使用JavaScript的saveAs進(jìn)行保存,各瀏覽器對(duì)saveAs支持,見(jiàn)下表。

代碼一:初始保存的代碼,只有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);//執(zhí)行保存,IE6,IE7,IE8有效
  OW.close();
 }

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

代碼二:采用下載方式保存頁(yè)面代碼。

 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 內(nèi)容 fileName 文件名 先在服務(wù)器生成頁(yè)面,然后再下載生成的頁(yè)面
 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 + "http://" + window.location.host + "/" + url;
    window.open(downloadUrl);//下載頁(yè)面
    //deleteFile(fileName);
   },
   error: function (msg) {
    alert("保存出錯(cuò)");
   }
  });
 }
  //保存頁(yè)面
  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);
    }
   }
   //生成要保存的頁(yè)面
   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;
  }
//下載頁(yè)面
  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";
   //通知瀏覽器下載文件而不是打開(kāi)
   System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
   System.Web.HttpContext.Current.Response.WriteFile(filePath);
  }

以上內(nèi)容就是本文關(guān)于execcommand兼容性問(wèn)題的全部敘述,希望大家喜歡。

相關(guān)文章

最新評(píng)論