0

Platform used

Front end : Angular

Backend : WebAPI

Requirement : Need to show the Save as dialog, once file is returned from the Server (API - ActionResult) to Angular (front end)

Explanation: Front end: Sending the data through FormData submit (action link) Back End: Returned the File from Action Result

Save as Dilaog

(Instead of changing the browser settings, need to handle it in code)

Browser settings permission

  • Possible duplicate of [How to make a browser display a "save as dialog" so the user can save the content of a string to a file on his system?](https://stackoverflow.com/questions/11336663/how-to-make-a-browser-display-a-save-as-dialog-so-the-user-can-save-the-conten) – Patricio Vargas Sep 09 '19 at 05:57
  • @PatricioVargas Saving the file is not problem. Have to show the "Save as Dialog" before download. I have tried the solution marked in that post. It doesn't show the "Save as dialog" before downloading it (doesn't override the browser settings) code: var pom = document.createElement('a'); pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent('Hello world!')); pom.setAttribute('download', 'test.txt'); if (document.createEvent) { var event = document.createEvent('MouseEvents'); event.initEvent('click', true, true); pom.dispatchEvent(event); } else { pom.click(); } – Ravi Sankar Sep 09 '19 at 07:09

1 Answers1

0

1) We can't change browser settings from javascript but still, you can prompt the dialog box with download attribute, check the below link

http://paxcel.net/blog/savedownload-file-using-html5-javascript-the-download-attribute-2/

this can be triggered from front end only, to automatically prompt the dialog box you have to make javascript button click from your controller

Dilipwk
  • 16
  • 1
  • 4
  • When the browser settings is in default state, the prompt doesn't get show even on download attribute **Code:** var pom = document.createElement('a'); pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent('Hello world!')); **pom.setAttribute('download', 'test.txt');** if (document.createEvent) { var event = document.createEvent('MouseEvents'); event.initEvent('click', true, true); pom.dispatchEvent(event); } else { pom.click(); }. Here i have added the download attribute in tag, still the file download automatically (didn't show prompt box) – Ravi Sankar Sep 09 '19 at 13:40
  • Seems like this is not possible – Dilipwk Sep 10 '19 at 14:27