Bulletproof function for file downloading from JavaScript. Prevent files opening when possible and avoids annoying empty windows in Chrome/Safari.