0

I'm working with MVC

I have a dialog in one of my views.

My dialog

$("#dialog-confirm").dialog({
            autoOpen: false,
            resizable: false,
            height: 390,
            width: 402,
            modal: true,
            buttons: {
                "Generate Report": function () {                        
                    $.ajax({
                        type: "GET",
                        url: '/Report/Report', 
                        data: 
                            { 
                                reportName : $('#dialogReportTypeID').val(),
                                reportData: $('#dialogPrintFormatSelector').val()"
                            }, 
                        dataType: 'html',
                        success: function(data)
                        {
                            $('body').html(data);
                        }
                    });

                    $(this).dialog("close");
                },
                Cancel: function () {
                    $("#cancel").text("");
                    $(this).dialog("close");
                }
            }
        });

The Dialog calls a different controller. The controller returns an ActionResult to AJAX. The ActionResult is either HTML or a `FileStream' that is intended to be downloaded.

The page gets re-written with $('body').html(data); but it seems like the serialized value of the FileStream only gets passed back. since i see it on the screen without any format, just data...

Q: how can I download the file comming from the Controller's Action Result?

fifamaniac04
  • 2,343
  • 12
  • 49
  • 72
  • You can't download a file this way. See http://stackoverflow.com/questions/13941358/c-sharp-mvc-return-json-or-file-from-ajax-call – Jasen Apr 23 '15 at 23:19

1 Answers1

0

If the Html is successfully found/created bind html with body else call javascript windows.open to retrieve the file.

Try Below

    $.ajax({
    ..
     success: function(data)
     {
      if( data.getResponseHeader('content-type').indexOf('text/html') >= 0 ){
        $('body').html(data);
        }
        else{
        window.open('http://'+path+data); 
        }        
     }
    ..
    })
Abbas Galiyakotwala
  • 2,949
  • 4
  • 19
  • 34