How to Use execCommand (, javascriptexec in JavaScript
This solution is only applicable to the asp.net mvc development environment, and other environments are for reference only.
Problem description: when this problem occurs during development, you can save the page by using the saveAs in JavaScript. The following table lists the supported saveAs in various browsers.
Code 1: The initial stored code is only supported by IE6, 7, and 8.
Function CmdSave () {var OW = window. open (''," _ blank "," "); var DD = new Date (); OW.doc ument. open (); var content = document. getElementById ("content "). innerHTML; OW.doc ument. write (content); var name = mineName + "-" + $ ("# selDate "). val () + ". htm "; OW.document.exe cCommand (" saveAs ", false, name); // run save, IE6, IE7, IE8 valid OW. close ();}
Solution: Considering the good download compatibility, it can also be used to save the page, so we adopted a solution like Mr Cheng page and then download the page.
Code 2: Save the Page code by downloading it.
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 file name is first generated on the server page, and then downloaded to the generated page function savePage (content, fileName) {$. ajax ({type: 'post', ype: '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 the public int BCYM (string content, string fileName) {string path = System. appDomain. currentDomain. baseDirectory; path = Path. combine (path, @ "Upload \ FXBB"); // clear the file foreach (string d in Directory. getFileSystemEntries (path) {if (File. exists (d) {File. delete (d) ;}// generate the page path to save = 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 the 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 the file 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"; // notifies the browser to download the file instead of opening the 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 all about execcommand compatibility in this article. I hope you will like it.