If I understand your question correctly you can't actually perform a file download with Ajax directly. You can request a file using Ajax but your Ajax response will contain the actual bits for the file. Due to JavaScript security limitations you won't be able to save those bits onto the user's computer with a file download dialog. For more information on why this is take a look at my blog post
here
I created the
Query File Download plugin (
Demo) which fixes the problem and provides some other nice features. It basically gives you a "full Ajax-like" experience for file downloads (complete with callbacks even) that isn't normally possible for file downloads. It also has some other handy features:
- User never leaves the same page they initiated a file download from whether it is successful or there is an error
- successCallback and failCallback functions allow for you to be explicit about what the UI behavior is in either situation
- In conjunction with jQuery UI a developer can easily show a modal telling the user that a file download is occurring, disband the modal after the download starts or even inform the user in a friendly manner that an error has occurred. See the
Demo for more example.