<p>I don't think you'll be able to use Craft's <a href="https://craftcms.com/classreference/services/HttpRequestService#sendFile-detail" rel="nofollow noreferrer"><code>HttpRequestService-&gt;sendFile()</code></a> for this.  But there are <a href="https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post">plenty</a> of <a href="https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax">non-Craft</a> <a href="https://stackoverflow.com/questions/3499597/javascript-jquery-to-download-file-via-post-with-json-data">ways</a> of <a href="https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request">doing this</a>, depending on your specific requirements.</p>

CC BY-SA 3.0
2017-01-10T16:00:45.317Z
thank you for your reply. I would definitely have a look at them and post here which method I went with.
Naveed
CC BY-SA 3.0
2017-01-10T00:10:03.783Z
2017-01-10T00:10:03.783Z
Brad Bell
<p>The following code doesn't seem to work when called using an Ajax or Task but it does work otherwise:</p>

<pre><code>craft()-&gt;request-&gt;sendFile($file, IOHelper::getFileContents($file), array('forceDownload' =&gt; true));
</code></pre>

<p>I was wondering, Is it not possible to force download a file using an Ajax or Task?</p>

<p>Any help much appreciated!</p>

<p>Many thanks</p>

<plugin-development><request>
Force Download a file using an Ajax request or Task
# Force Download a file using an Ajax request or Task

(Asked by: username_0 on 2017-01-09)

The following code doesn't seem to work when called using an Ajax or Task but it does work otherwise:
craft()->request->sendFile($file, IOHelper::getFileContents($file), array('forceDownload' => true));

I was wondering, Is it not possible to force download a file using an Ajax or Task?
Any help much appreciated!
Many thanks

**Tags:** plugin-development, request

---

## 1 answers

### Answer by username_1 on 2017-01-10. Score: 0 | Accepted answer

I don't think you'll be able to use Craft's [`HttpRequestService->sendFile()`](https://craftcms.com/classreference/services/HttpRequestService#sendFile-detail) for this. But there are [plenty](https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post) of [non-Craft](https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax) [ways](https://stackoverflow.com/questions/3499597/javascript-jquery-to-download-file-via-post-with-json-data) of [doing this](https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request), depending on your specific requirements.

> **Comments:**
> **username_0**: thank you for your reply. I would definitely have a look at them and post here which method I went with.  

---

