eligrey / FileSaver.js

An HTML5 saveAs() FileSaver implementation
https://eligrey.com/blog/saving-generated-files-on-the-client-side/
Other
21.42k stars 4.38k forks source link

html 导出来 word 文件在微信中打不开,提示文件已损坏, 直接在电脑上下载可以打开 #716

Open chenpeizhen opened 3 years ago

chenpeizhen commented 3 years ago

gTranslate: The word file exported from html cannot be opened in WeChat, indicating that the file is corrupted, and it can be opened by downloading it directly on the computer

const fileContent = mhtml.top.replace(
      "_html_",
      mhtml.head.replace() + mhtml.body.replace("_body_", document.querySelector('#myMessage').innerHTML),
 )
 const blob = new Blob([fileContent], {
      type: "application/msword;charset=utf-8",
 })

saveAs(blob, title)
makc commented 3 years ago

@chenpeizhen what seems to be the problem 😂 why are you saving some garbage as msword file, and then act surprised that it wont open?

jimmywarting commented 3 years ago

i suspect application/msword is a binary format.

you are basically taking plain text and use: document.querySelector('#myMessage').innerHTML this can include HTML elements and what not. filesaver is not a conversion tool.

you might want to save it as:

 const blob = new Blob([fileContent], {
      type: "text/html",
 })

saveAs(blob, title + '.html')
HarrisZeng0631 commented 2 years ago

@jimmywarting She means after download the .doc file can't open on wechat and she was using iphone 12(maybe) .