eligrey / eligrey/FileSaver.js

Save same structure when using contenteditable="true"

Open
#507 1 comment 0 reactions 0 assignees View on GitHub
help wanted Make a Blob
Dominant language
JavaScript
Stars
22k
Forks
4.3k
PR merge metrics
No merged PRs in 30d

Description

Currently I build small app allow user to load file and edit them
![chrome_2018-12-09_07-20-16](https://user-images.githubusercontent.com/13591213/49692051-e28cd200-fb82-11e8-8bfe-e8819c37777d.png)

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

```js
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

```

Result
![code_2018-12-09_07-26-23](https://user-images.githubusercontent.com/13591213/49692082-d48b8100-fb83-11e8-8024-cfa9cd55d73a.png)

Try with this
```js
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](https://user-images.githubusercontent.com/13591213/49692113-ca1db700-fb84-11e8-9bb4-5c58fff0048c.png)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.