karl
karl

Reputation: 73

jQuery post with FileStreamResult as return value

I'm quite new with jquery and asp.net mvc. My problem is that I'm calling a method in a controller that returns a FileStreamResult. This is working fine, but when I'm calling it with the jQuery post it doesn't work. I can see with vs debug tool that the progam is exectuting the method. Therefor I think it has something to do with that my jQuery call should take care of the return parameter? Somenoe?

The jQuery code:

    <script type="text/javascript">
    function createPPT() {
            $.post("<%= Url.Action( "DownloadAsPowerpoint", "RightMenu" )%>");
    }
    </script>

The method in the controller:

    public ActionResult DownloadAsPowerpoint()
    {
        Stream stream; 
        //...
        HttpContext.Response.AddHeader("content-disposition", "attachment; filename=presentation.pptx");

        return new FileStreamResult(stream, "application/pptx");
    }

Could someone explain and give me some example code?

Upvotes: 5

Views: 7143

Answers (1)

wassertim
wassertim

Reputation: 3136

Use $.ajax() method, because you don't send any parameters:

    function createPPT() {
        //Show waiting dialog here
        $.ajax({
            url: '<%=Url.Action("DownloadAsPowerpoint") %>',
            method:'GET',
            success: function (fileStream) {
                //Hide waiting dialog here
                alert(fileStream); //This is your filestream
            }
        });
        return false;
    }

Upvotes: 3

Related Questions