1

I want to download file via ajax get request which calls coldfusion function, but the problem is that in "network" tab in dev tools I can "see" the content, but it doesn't download it.

Here is my coldfusion function which is stored in cfc:

<cffunction name="download" access="remote" output="true">
    <cfset file_name = "Test.xlsx">
    <cfheader name='Content-Disposition' value='attachment; filename=#file_name#' charset='utf-8'> 
    <cfcontent type="application/msexcel" file="#file_name#" deletefile="false">
</cffunction>

Here is my ajax call:

<script>
        $.ajax({
            method: "GET",
            url: "ses/dev.cfc",
            data: {
                method: 'download'
            },
            cache: false,
            success: function(data) {
                console.log("Downloaded");
            },
            error: 'Not downloaded'
        });
    </script>

And here is my result: https://i.stack.imgur.com/Q8qNy.jpg

T2Admin
  • 147
  • 2
  • 14
  • 4
    If you want this to download the file to your computer, you need to make an HTTP GET request to the server, not an XMLHttpRequest (Ajax) GET. – Adrian J. Moreno Mar 09 '17 at 23:22
  • I think you will need to do this as a .cfm and not as a function in a .cfc. – snackboy Mar 10 '17 at 02:57
  • Why are you complicating a simple matter of file downloading by adding javascript to the equation? – Dan Bracuk Mar 10 '17 at 04:08
  • 1
    @DanBracuk Can I ask you how would you do it ? – T2Admin Mar 10 '17 at 06:56
  • I would have a Cold Fusion page (.cfm) where the last two lines of code are the `` and `` tags. – Dan Bracuk Mar 10 '17 at 12:37
  • It is not really specific to ColdFusion. Search the archives for jQuery ajax download. Lots of threads explaining the reason and possible alternatives, such as http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax – Leigh Mar 10 '17 at 13:36
  • @Leigh it's specific to ColdFusion if yoiu're trying to download a file from a folder outside of the webroot and if you need to verify the user's logged in and has access to the file. – Adrian J. Moreno Mar 10 '17 at 16:11
  • @AdrianJ.Moreno - True. I just meant the general issue is not CF specific. Least not from what they posted. – Leigh Mar 10 '17 at 18:02

1 Answers1

1

I think you'll that downloading function should be remove inside Ajax, and move that to different process... maybe call it on different URL something like this:

http://websiteurl.com/download.php?file=xxxxx&sec_string=xxxxxx etc

I hope this make sense :)

jovenb
  • 120
  • 2
  • 9