Thank you. We appreciate your patience.
Adding an exit() call at the end of the simple form handler above solves the corrupted file issue.
Is it possible you could share a simple ajax example?
When we use ajax, the document is sent as part of the response ( it appears in the browser console ) and is not downloaded. And we get a jquery error:
SyntaxError: Unexpected token P in JSON at position 0