How to solve the use of execcommand in JavaScript (_javascript technique

Source: Internet
Author: User
Tags httpcontext save file

This solution only adapts to the ASP.net MVC development environment, and other environments are for reference only.

Problem Description: Encountered in the development of such requirements, save the page, usually using JavaScript SaveAs to save, the browser to SaveAs support, see the table below.

Code one: Initial saved code, only ie6,7,8 support.

 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);//execute save, IE6,IE7,IE8 valid
  ow.close ();
 }

Solution: Considering the download compatibility is good, can also play the role of saving the page, so the use of the page, and then download the page such a solution.

Code two: Save the page code with the download method.

 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 content filename FileName first generates page on server, then downloads generated page function savepage (content, FileName) {$.ajax ({type: ' Post ', D Atatype: ' 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);//download page//deletefile (fileName);
   Error:function (msg) {alert ("Save Error");
 }
  }); //Save page public int BCYM (string content, String fileName) {string path = System.AppDomain.CurrentDomain.BaseDirectory;
   Path = Path.Combine (path, @ "UPLOAD\FXBB"); Empty Save file Folder file foreach (String d in directory.getfilesystementries (path)) {if (file.exists (d)) {FILE.D
    Elete (d);
   }//Generate the page to be saved 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;
   ///download page 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");//path//download file in the form of a character stream FileStream fs = new FileStream (Filepa
   th, 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"; Notifies the browser to download the file instead of opening System.Web.HttpContext.Current.Response.AddHeader ("Content-disposition", "attachment;
   Filename= "+ httputility.urlencode (FileName, System.Text.Encoding.UTF8));
  System.Web.HttpContext.Current.Response.WriteFile (FilePath); }

The above content is this article about the ExecCommand compatibility question entire narration, hoped everybody likes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.