Change the content type of the popup

This question brought a new one:

I have a html page and I need to change its content type when the user clicks the Save button so that the browser will prompt to save the file to disk

I was doing this on the server side to offer an "excel" version of the page (which is basically an 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 client side with javacript, but I can't seem to do it.

This is what I have 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>

      

+2


source to share


2 answers


You can try using a hidden iframe. When the user clicks the save button, update the iframe src anywhere you store / generate the excel file. A save dialog will appear.



0


source


As said in the link you posted, only the supported mime types (in IE at least) are plain text and HTML. You must make a server call to use a different mime type. You may be able to use an ActiveX control, but not browser compatible.



0


source







All Articles