这个问题带来了一个新问题:
我有一个 html 页面,当用户按下“保存”按钮时,我需要它来更改内容类型,以便浏览器提示将文件保存到磁盘
我一直在服务器端这样做以提供页面的“excel”版本(基本上是一个 html 表)
<c:if test="${page.asExcelAction}">
<%
response.setContentType("application/vnd.ms-excel");
%>
我现在想做的是做同样的事情,但是在客户端使用 javacript 但我无法做到。
这是我到目前为止所得到的:
<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>