Link to home
Start Free TrialLog in
Avatar of amillyard
amillyardFlag for United Kingdom of Great Britain and Northern Ireland

asked on

open a doc in a new browser window

attached is the script I use to open respective document.

this replaces the current .aspx file and opens the doc.

would like to open the doc in a different browser window (or tab) -- initiated via server-side scripting.

how is achieved please?
private void ViewDocument(string viewingFileNameExt)
        {
            switch (viewingFileNameExt)
            {
                case ".pdf": Response.ContentType = "Application/pdf";
                    break;
                case ".txt": Response.ContentType = "text/plain";
                    break;
                case ".jpg": Response.ContentType = "image/JPEG";
                    break;
                case ".gif": Response.ContentType = "image/GIF";
                    break;
                case ".doc": Response.ContentType = "Application/msword";
                    break;
                case ".xls": Response.ContentType = "Application/x-msexcel";
                    break;
                case ".ppt": Response.ContentType = "application/vnd.ms-powerpoint";
                    break;
                case ".pps": Response.ContentType = "application/vnd.ms-powerpoint";
                    break;
                case ".pptm": Response.ContentType = "application/vnd.ms-powerpoint.presentation.macroEnabled.12";
                    break;
                case ".pptx": Response.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                    break;
                case ".xlsm": Response.ContentType = "application/vnd.ms-excel.sheet.macroEnabled.12";
                    break;
                case ".xlsx": Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    break;
                case ".docm": Response.ContentType = "application/vnd.ms-word.document.macroEnabled.12";
                    break;
                case ".docx": Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    break;
            }
 
            Response.WriteFile(hostedSubFolderLocation + "\\" + hostedFileName);
            Response.End();
        }

Open in new window

Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

That is not possible. When the server code starts, it has already been determined where the page will open. Once the request has been sent, it's too late to change where the response will go.

You have two alternatives:

1. Open the page in a new window instead of doing a postback. This has to be done in the browser before the page is requested.

2. Return a page that contains a Javascript that opens the document in a new window. You can use the Page.ClientScript.RegisterStartupScript to add Javascript code to the page.
Avatar of amillyard

ASKER

GreenGhost: ok, filename is picked from a datagrid row -- so that button press initiates the db call to collect the filename / location details -- when then calls the above procedure to display the document.

just confirming -- does that mean option 2 is more suited?
when I try the attached I am getting the current web-page and document merged into the same web-page (same browser window)
Page.ClientScript.RegisterStartupScript(GetType(), "test", "window.open('" + hostedSubFolderLocation + "','Height=300px,Width=700px,menubar=No,toolbar=no,scrollbars=yes');");

Open in new window

You forgot the target parameter in the window.open call. Also the width and height of the window should not have the css unit "px".

Page.ClientScript.RegisterStartupScript(GetType(), "test", "window.open('" + hostedSubFolderLocation + "','_blank','Height=300,Width=700,menubar=No,toolbar=no,scrollbars=yes');");

Have you removed the Response.WriteFile call?
"Have you removed the Response.WriteFile call?"   do I need to remove this scripting?  if so, how would I then display the document?  (or is the naem of the file added to end of hostedSubFolderLocation ?
ASKER CERTIFIED SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
GreenGhost:  have made the adjustments as attached.

but 1. no new browser opens, and 2. the document and web-page are merged together (in the same browser window)
        private void ViewDocument(string viewingFileNameExt)
        {
            switch (viewingFileNameExt)
            {
                case ".pdf": Response.ContentType = "Application/pdf";
                    break;
                case ".txt": Response.ContentType = "text/plain";
                    break;
                case ".jpg": Response.ContentType = "image/JPEG";
                    break;
                case ".gif": Response.ContentType = "image/GIF";
                    break;
                case ".doc": Response.ContentType = "Application/msword";
                    break;
                case ".xls": Response.ContentType = "Application/x-msexcel";
                    break;
                case ".ppt": Response.ContentType = "application/vnd.ms-powerpoint";
                    break;
                case ".pps": Response.ContentType = "application/vnd.ms-powerpoint";
                    break;
                case ".pptm": Response.ContentType = "application/vnd.ms-powerpoint.presentation.macroEnabled.12";
                    break;
                case ".pptx": Response.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                    break;
                case ".xlsm": Response.ContentType = "application/vnd.ms-excel.sheet.macroEnabled.12";
                    break;
                case ".xlsx": Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    break;
                case ".docm": Response.ContentType = "application/vnd.ms-word.document.macroEnabled.12";
                    break;
                case ".docx": Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    break;
                case ".tif": Response.ContentType = "image/tiff";
                    break;
                case ".tiff": Response.ContentType = "image/tiff";
                    break;
            }
 
            Page.ClientScript.RegisterStartupScript(GetType(), "test", "window.open('" + hostedSubFolderLocation + "\\" + hostedFileName + "','_blank','Height=400,Width=700,menubar=No,toolbar=no,scrollbars=yes');");
 
            //Response.WriteFile(hostedSubFolderLocation + "\\" + hostedFileName);
            //Response.End();
        }

Open in new window

Also comment out the code that sets the content type. As the document is not returned in the response, you should not change the content type of the response.

The url that you use in the window.open method is a virtual path, so it uses slash (/) as separator, not backslash (\).

You have to add another parameter in the RegisterStartupScript call. You need to send the value true for the addScriptTags parameter, so that a script tag is added around the Javascript code.
many thanks :-)