I have a custom solution where I open a SP Dialog box through Javascript. From this dialog, I want to generate a file and sent it to the user when he is clicking on a button, than close the dialog.

All the pieces of my puzzle are working individually, but when stitch together, the dialog box never close.

Sending the dynamic file (data) on button click:

HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=Export.csv");
HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";

Works great, then I want to close my dialog box using the following code:

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ClosingScript",  "<script type=\"text/javascript\">window.frameElement.commonModalDialogClose(1, null);</script>");

It will not close the dialog. I removed the file generation and the dialog will close immediately. I've tried numerous combination of code, even handler and more JavaScript, without success. Maybe someone had some new/fresh idea for me?


2 Answers 2


The http response stream can only be one content-type. By sending the binary data, you are effectively turning the stream over to the client operating system as an 'octet-stream' cannot process html or javascript.

Requests like this often are done by using the initial page to launch a popup or kick of a client side process that starts the file transfer. The initial page closes once it is satisfied that the browser is handling the download (you have to define your own criteria for that)

    function OpenMyModalWithCallback()
    var options = {
        url: '/{your site}/_layouts/Upload.aspx?List={1F68B750-98D7-4523-8C3C-F662E1E2386F}&RootFolder=',
        tite: 'Add new document',
        allowMaximize: false,
        showClose: true,
        width: 750,
        height: 550,
        dialogReturnValueCallback: CallbackFunctionToCall};

SP.UI.ModalDialog.showModalDialog(options); }

function CallbackFunctionToCall(dialogResult, returnValue) 

{ if(dialogResult == SP.UI.DialogResult.OK) { SP.UI.Notify.addNotification('OK was clicked'); } if(dialogResult == SP.UI.DialogResult.cancel) SP.UI.Notify.addNotification('Cancel was clicked!'); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.