I am trying to use express to send a file I have generated on my node.js server to the client. The file is a .docx
file that gets generated perfectly (I can open it on the local machine without any problems). After sending the file to the client, the download prompt shows up as normal and it knows to save it as a .docx
document.
However, the file is a few KB's larger than the original file. When trying to open it, I get an error that Word can not read the contents. Which, when comparing the files is logical, since it seems that on transferring, express' res.download()
converts the special characters (like á Ë, etc...) to something like �.
Here is the code for sending the file and the response Headers I get in my browser:
var filePath = '.tmp/' + req.options.name;
doc.output({name: filePath});
filePath = path.join(__dirname, '../../..', filePath);
if(!fs.existsSync(filePath))
return res.sendData(null, 500, lang.somethingWentWrong);
res.download(filePath, "test", function(err){
if(err) return next(err);
});
And the server's Response Headers:
HTTP/1.1 200 OK
X-Powered-By: Express
content-disposition: attachment; filename="test"
Accept-Ranges: bytes
ETag: "0-1410275362000"
Date: Tue, 09 Sep 2014 15:09:22 GMT
Cache-Control: public, max-age=0
Last-Modified: Tue, 09 Sep 2014 15:09:22 GMT
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
content-length: 110476
Connection: keep-alive
The MIME type seems to set correctly, so why is my content being converted? Any help would be appreciated!
UPDATE
It turns out that the templater I used to create the word documents (docxtemplater by edi9999) adds special characters to the .docx
file. Word can still interpret this, but as soon as I try to send the file it changes the characters. If I copy the contents to a new word file and try to send that it also sends perfectly, so it seems that these extra characters are not necessary. Can I solve this using Express
or do I need to contact the developer of the template engine?