0
我正在創建一個HTML頁面,用戶單擊「下載」按鈕後可以將其下載爲.docx。我設法下載.docx文件,但是當我嘗試打開它時,彈出窗口說「文件已損壞,無法打開」。使用經典ASP下載HTML頁面爲.docx
Response.ContentType = "application/vnd.ms-word"
Response.ContentType = "application/octet-stream"
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
我如何從HTML頁面的每個數據(圖像和表格),並在擴展的docx MS Word中使用傳統的ASP:
據透露,我一直在使用這些嘗試?
<%if Request("btn_download")<>"" Then
'Only set the headers if this is request for download
Response.Buffer = TRUE
Response.ContentType = "application/vnd.ms-word"
Response.AddHeader "content-disposition","attachment; filename=thefile.docx;"
end if
%>
<!DOCTYPE html>
<html>
<head>
<%response.write("<h1><center>Welcome!</h1>")%>
<center><img src="http://www.themelab.com/wp-content/uploads/smiley.jpg" alt="Smiley" style="width:auto;height:auto;"></center>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h1> </h1>
<table>
<tr>
<th>Name</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
<%if Request("btn_download")="" Then
'Show the download button only if the button has not been clicked.
%>
<p>Click the button below to copy the page as word document.</p>
<form>
<input type="submit" name="btn_download" value="Download">
</form>
<%End If%>
</body>
</html>
你不能只給它一堆HTML,並且在下載請求時,設置頭文件以下載一個名爲docx的擴展名的文件。在您實際生成文檔的代碼中沒有任何地方,因此它以空白出現在這裏並不令人意外。你甚至google'd DOCX格式,以及如何動態生成它?或者你是否認爲要「說服它,當然會起作用」? – zerohero
你可以給一堆html並將其作爲docx打開。但只有MS字。因爲MS詞試圖將html呈現爲單詞doc。 – ulluoink
因此,您可以嘗試的是將HTML代碼寫入服務器上的變量中,然後將該變量的內容通過頁面末尾的Header Infos發送到客戶端。或者將html保存在服務器上的一個文件中,該文件以「docx」結尾,然後將文件發送到瀏覽器。當客戶端安裝了MS字時,它看起來就像是一個word文檔,儘管它是一個帶有html的文本文件。 – ulluoink