When I download a text file, .txt gets added to filename. How do I prevent this?
You can only in general prevent this if you control the server. 

I assume you're talking about downloading using a browser - in which case the web server controls the file type and naming using the Content-Type MIME indication as well as the Content-Disposition header for specifying the file name.