无法使用C#下载docx文件

Jul*_*ary 8 c# download docx

这是我的代码,我正在尝试下载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();
            } 
Run Code Online (Sandbox Code Playgroud)

和返回内容类型给出,docx文件的内容类型:

"application/ms-word"
Run Code Online (Sandbox Code Playgroud)

其中,如果sTempPath + sCreateFileName是文件的整个路径.

更新: 我尝试过内容类型:

application/vnd.openxmlformats-officedocument.wordprocessingml.document
Run Code Online (Sandbox Code Playgroud)

这不起作用.

Roy*_*tus 10

正确的MIME类型DOCX不是application/msword,但application/vnd.openxmlformats-officedocument.wordprocessingml.document.

您指定的MIME类型适用于DOC文件.

你也许想把a Response.Flush()和a Response.End()代替CompleteRequest().