Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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";
HttpContext.Current.Response.BinaryWrite(data);
HttpContext.Current.Response.Flush();

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?

Thanks!

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

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)

share|improve this answer
add comment
    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!'); }

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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