Can JavaScript Force Download?

I understand that I can force a file to be saved instead of opened the server sends out appropriate headers.

But can this be done client-side with JavaScript?

<a href="pdf.pdf" onclick="forceDownload(pdf.pdf);return false;">Force Download of PDF (do NOT open in browser)</a>

Open in new window

LVL 16
Who is Participating?
You cannot use javascript so force download a file. The response headers required for force download cannot be added using javascript and has to be done using a server side script.
You cannot do it through Javascript. But you can have an intermediate Server page which can access the download file on your behalf, and can write the Forced download entries in the response headers.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.