in reply to Web form creates txt file and downloads scalar as a file

You should output proper HTTP headers instead of what you're printing.

For a start, HTTP headers are delimited by \r\n, not \n alone. Also, you might need to output a HTTP 200 OK status code.