admin管理员组文章数量:1332865
So that I can implement a live preview for uploadable HTML files, I need help decoding the base64 string.
When I pass a file to the input element and read it with FileReader()
, I get a base64 encoded string.
What do I have to do to convert it to HTML/TXT?
I already found some stuff about decoding pictures, which unfortunately didn't help me.
handleFileUpload() {
this.file = this.$refs.file.files[0];
let reader = new FileReader();
reader.addEventListener("load", function() {
this.html = atob(reader.result);
}.bind(this), false);
reader.readAsDataURL(this.file);
},
Output:
data:text/html;base64,PGh0bWwgeG1sbnM6dj0idXJuOnNjaGVtYXMtbWljcm9zb2Z0LWNvbTp2bWwiDQp4bWxuczpvPSJ1cm46c2NoZW1hcy1taWNyb3NvZnQtY29tOm9mZmljZTpvZmZpY2UiDQp4bWxuczp4PSJ1cm46c2NoZW1hcy1taWNyb3NvZnQtY29tOm9mZmljZTpleGNlbCINCnhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy9UUi9SRUMtaHRtbDQwIj4NCg0KPGhlYWQ+DQo8bWV0YSBodHRwLWVxdWl2PUNvbnRlbnQtVHlwZSBjb250ZW50PSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9d2luZG93cy0xMjUyIj4NCjxtZXRhIG5hbWU9UHJvZ0lkIGNvbnRlbnQ9RXhjZWwuU2hlZXQ+DQo8bWV0YSBuYW1lPUdlbmVyYXRvciBjb250ZW50PSJNaWNyb3NvZnQgRXhjZWwgMTUiPg0KPGxpbmsgaWQ9TWFpbi1GaWxlIHJlbD1NY......
So that I can implement a live preview for uploadable HTML files, I need help decoding the base64 string.
When I pass a file to the input element and read it with FileReader()
, I get a base64 encoded string.
What do I have to do to convert it to HTML/TXT?
I already found some stuff about decoding pictures, which unfortunately didn't help me.
handleFileUpload() {
this.file = this.$refs.file.files[0];
let reader = new FileReader();
reader.addEventListener("load", function() {
this.html = atob(reader.result);
}.bind(this), false);
reader.readAsDataURL(this.file);
},
Output:
data:text/html;base64,PGh0bWwgeG1sbnM6dj0idXJuOnNjaGVtYXMtbWljcm9zb2Z0LWNvbTp2bWwiDQp4bWxuczpvPSJ1cm46c2NoZW1hcy1taWNyb3NvZnQtY29tOm9mZmljZTpvZmZpY2UiDQp4bWxuczp4PSJ1cm46c2NoZW1hcy1taWNyb3NvZnQtY29tOm9mZmljZTpleGNlbCINCnhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy9UUi9SRUMtaHRtbDQwIj4NCg0KPGhlYWQ+DQo8bWV0YSBodHRwLWVxdWl2PUNvbnRlbnQtVHlwZSBjb250ZW50PSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9d2luZG93cy0xMjUyIj4NCjxtZXRhIG5hbWU9UHJvZ0lkIGNvbnRlbnQ9RXhjZWwuU2hlZXQ+DQo8bWV0YSBuYW1lPUdlbmVyYXRvciBjb250ZW50PSJNaWNyb3NvZnQgRXhjZWwgMTUiPg0KPGxpbmsgaWQ9TWFpbi1GaWxlIHJlbD1NY......
Share
Improve this question
asked Nov 6, 2019 at 14:00
Philipp NiesPhilipp Nies
9694 gold badges21 silver badges40 bronze badges
2 Answers
Reset to default 3Instead of readAsDataURL
, you should use readAsText
, so that you get the text instead of a blob.
handleFileUpload() {
this.file = this.$refs.file.files[0];
let reader = new FileReader();
reader.addEventListener("load", function() {
this.html = reader.result;
}.bind(this), false);
reader.readAsText(this.file);
},
You can preview your html file, with the answer of Math Ellen by put the result in an html object like this :
<object type="text/html"
data="file.html"
width="250"
height="200">
</object>
本文标签: javascriptVueJS decode base64 html stringStack Overflow
版权声明:本文标题:javascript - VueJS decode base64 html string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289184a2447482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论