views:

216

answers:

2

This question brought a new one:

I have a html page and I need it to change the content type when the user press "save" button so the browser prompt to save the file to disk

I've been doing this in the server side to offer "excel" versions of the page ( which is basically a html table )

    <c:if test="${page.asExcelAction}">
    <% 
        response.setContentType("application/vnd.ms-excel");
    %>

What I'm trying to do now is to do the same, but in the client side with javacript but I can't manage to do it so.

This is what I've got so far:

<html>
    <head>
        <script>
            function saveAs(){
                var sMarkup =  document.getElementById('content').innerHTML; 
                //var oNewDoc = document.open('application/vnd.ms-excel');        
                var oNewDoc = document.open('text/html');        
                oNewDoc.write( sMarkup );
                oNewDoc.close();
            }
        </script>
    </head>
<body>
<div id='content'>
    <table>
        <tr>
            <td>Stack</td>
            <td>Overflow</td>
        </tr>
    </table>
</div>    
<input type="button" value="Save as" onClick="saveAs()"/>
</body>
</html>
A: 

You could try using a hidden iframe. When the user clicks save, update the iframe src to wherever you are storing/generating the excel file. A save dialog should pop up.

Byron Whitlock
A: 

As was said in the link you posted, the only mime types currently supported (at least in IE) are plain text and HTML. You have to make a server-side call to use a different mime type. You might be able to use an ActiveX control, but that's not cross browser compatible.

adam0101