Search code examples
javascriptajaxcoldfusioncfmlcoldfusion-11

Coldfusion download file


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.sstatic.net/Q8qNy.jpg


Solution

  • 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 :)