title: Is there any way to 'simulate' right-click save-as command or force download of file in the browser with JavaScript?
body: <p>I have this situation where we have media files stored on a global CDN. Our web app is hosted on it's own server and then when the media assets are needed they are called from the CDN url. Recently we had a page where the user can download file attachments, however some of the file types were opening in the browser instead of downloading (such as MP3). The only way around this was to manually specify the HTTP response to attach the file but the only way I could achieve this was to download the file from CDN to my server and then feed it back to the user, which defeats the purpose of having it on the global CDN. Instead I am wondering if there is some client side solution for this?</p>
  
  <p>EDIT: Just found this somewhere, though I'm not sure if it will work right in all the browsers?</p>
  
  <pre><code>&lt;body&gt;
  &lt;script&gt;
  function downloadme(x){
  myTempWindow = window.open(x,'','left=10000,screenX=10000');
  myTempWindow.document.execCommand('SaveAs','null','download.pdf');
  myTempWindow.close();
  }
  &lt;/script&gt;
  
  &lt;a href=javascript:downloadme('/test.pdf');&gt;Download this pdf&lt;/a&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>RE-EDIT: Oh well, so much for that idea -> <a href="https://stackoverflow.com/questions/833015/does-execcommand-saveas-work-in-firefox/833024#833024">Does execCommand SaveAs work in Firefox?</a></p>