eligrey/FileSaver.js

Save same structure when using contenteditable="true"

Open

#507 opened on Dec 9, 2018

View on GitHub
 (1 comment) (0 reactions) (0 assignees)JavaScript (21,193 stars) (4,366 forks)batch import
Make a Blobhelp wanted

Description

Currently I build small app allow user to load file and edit them chrome_2018-12-09_07-20-16

The problem I have is when I click on download button the data save to the file is not the same as old structure since I'm using $("pre code").each when user upload a file to highlight it. Try with $("code").text() & $("code").html() but not giving me the result i want

    fileUpload(event) {
        const reader = new FileReader();
        console.log(event.srcElement.files[0]);
        this.fileName = event.srcElement.files[0].name;
        reader.readAsText(event.srcElement.files[0]);
        const me = this;
        reader.onload = function () {
            me.content = reader.result;
            hljs.configure({useBR: true});
             $(document).ready(function () {
                $("pre code")
                    .each(function (i, block) {
                        hljs.highlightBlock(block);
                    });
            });
        };
    }

    saveFile() {
        const blob = new Blob([$("code").text()], {type: "text/plain;charset=utf-8"});
        filesaver.saveAs(blob, this.fileName);
    }

Contain of the file

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:xlink="http://www.w3.org/1999/xlink" exclude-result-prefixes="msxsl xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

</xsl:stylesheet>

Result code_2018-12-09_07-26-23

Try with this

   filesaver.saveAs(
            new Blob(
                [(new XMLSerializer).serializeToString($("code").html())]
              , {type: "application/xhtml+xml;charset=" + document.characterSet}
          )
          , "document.xhtml"
      );

And I get following error

chrome_2018-12-09_07-33-47

Contributor guide