就像我的標題我該怎麼做那件事? 我試圖編寫像下面(文件view_report.php)如何重命名使用Javascript和PHP標題下載的文件?
<?php
header('Content-Disposition: inline;filename=report_loader.txt');
?>
<div id="reportTable">
<table>
<thead>
<tr>
<th>No.</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php foreach($reports->result() as $rows): ?>
<tr>
<th><?php $rows->no; ?></th>
<th><?php $rows->name; ?></th>
</tr>
</tbody>
</table>
</div>
<div class="row">
<input type="button" class="btn btn-success btn-sm" value="Generate Excel" id="exportExcel" />
</div>
<script>
$("#exportExcel").click(function (e) {
window.open('data:application/vnd.ms-excel,' + $('#reportTable').html());
e.preventDefault();
});
</script>
編輯 這是我的全部代碼。我已經嘗試過了。我在本網站提到了一些問題,但我並沒有爲我清理解決方案。我其實曾嘗試使用Content-Disposition: inline
和Content-Disposition: attachment
。但仍然沒有工作。瀏覽器(我在Chrome和Firefox上試過),它返回隨機文件名,而不是我想要的文件名。任何人都可以幫我找到這個解決方案嗎?
有[可能在Chrome中失敗](http:// blog。tylerholmes.com/2008/05/http-headers-content-type-and-content.html),它要求將content-type設置爲,例如'application/octet-stream',以便從'content-disposition'並且不會從URL中猜出。 –
如果我使用'application/octet-stream'或'application/vnd.ms-excel' – gultomicro