这是我的代码,我从几个小时以来一直在尝试下载 docx 文件。但没有成功。我可能落后的地方,需要一点提示。
if (File.Exists(sTempPath + sCreateFileName))
{
FileInfo file =new FileInfo(sTempPath + sCreateFileName);
Response.ClearContent();
// LINE1: Add the file name and attachment, which will force the open/cancel/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
// Add the file size into the response header
Response.AddHeader("Content-Length", file.Length.ToString());
// Set the ContentType
Response.ContentType = ReturnExtension(file.Extension.ToLower());
// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(sTempPath + sCreateFileName);
// End the response
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
并返回内容类型给出 docx 文件的内容类型:
"application/ms-word"
如果 sTempPath+sCreateFileName 是文件的整个路径。
更新: 我尝试了内容类型:
application/vnd.openxmlformats-officedocument.wordprocessingml.document
这是行不通的。