0

I'm in trouble with saving a word in Javascript ! I want to save the document as "x" by concatenating several vars. I tried the following:

    <script>// SAVE AS WORD
    var fname = document.getElementsByName("fname")[0].value;
    var name = document.getElementsByName("name")[0].value;
    var company = <?php echo json_encode($company); ?>;
    
   function exportHTML(){
   var header = "<html xmlns:o='urn:schemas-microsoft-com:office:office' "+
        "xmlns:w='urn:schemas-microsoft-com:office:word' "+
        "xmlns='http://www.w3.org/TR/REC-html40'>"+
        "<head><meta charset='utf-8'><title>Export HTML to Word Document with JavaScript</title></head><body>";
   var footer = "</body></html>";
   var sourceHTML = header+document.getElementById("pdf").innerHTML+footer;
   
   var source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML);
   var fileDownload = document.createElement("a");
   document.body.appendChild(fileDownload);
   fileDownload.href = source;
   **fileDownload.download = concat(fname,'-',name,'-LM-',company,'.doc');**
   fileDownload.click();
   document.body.removeChild(fileDownload);
}
</script>

The save-as line is the following :

   **fileDownload.download = concat(fname,'-',name,'-LM-',company,'.doc');**

Any idea ? Thanks a lot from France !

4
  • So what's the trouble, exactly? What goes wrong with the code? P.S. this appears to export HTML, so it's not really a Word doc at all. (Word may be able to open it, but giving it a .doc extension and a Word Mime Type is misleading and may even be confusing to Word when it tries to open it.) Commented Jul 9, 2020 at 9:23
  • @ADyson the code works perfectly since ".doc" is added in the file-name save as. See below lariuss' answer, it fixed the save as issue I had. Commented Jul 9, 2020 at 9:29
  • " since ".doc" is added in the file-name" ...yes I can see that, but that wasn't my point. My point was that while you've given the a file a .doc extension, it's not really a .doc file. It's a HTML file pretending to be a Word doc. While this may work, if Word is tolerant of it, it's generally not good practice to give a file a misleading extension. Commented Jul 9, 2020 at 9:31
  • Thanks a lot @ADyson for your input. I'll run tests on several devices to see if an error occurs. If so, I'll consider switching to another method. Commented Jul 9, 2020 at 10:02

1 Answer 1

2

concat is a method of String.prototype, so use it like this:

fileDownload.download = fname.concat('-', name, '-LM-', company, '.doc')

Or you can use the concatenation operator + :

fileDownload.download = fname + '-' + name + '-LM-' + company + '.doc';

Or you can use template strings:

fileDownload.download = `${fname}-${name}-LM-${company}.doc`;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.