2013-02-06 26 views
0

我已經編寫代碼,在代碼後面下載它後打開一個Word文檔。文檔打開正常,但不是以Word格式保存。當我打開它時,它會要求選擇打開文件的格式。從後面的代碼下載後打開一個word文件(asp.net)

的代碼如下:

string FullFilePath = "D:\\ASP\\ASP.doc"; 
FileInfo file = new FileInfo(FullFilePath); 

if (file.Exists) 
{ 
    Response.ContentType = "application/vnd.ms-word"; 
    Response.AddHeader("Content-Disposition", "inline; filename=\"" + txtDate.Text + "\""); 
    Response.AddHeader("Content-Length", file.Length.ToString()); 
    Response.TransmitFile(file.FullName); 
} 
+0

嘗試response.contenttype =「application/doc」 –

回答

2

設置你的內容類型application/msword

參見:Microsoft Office MIME Types

發送文件名時,您沒有指定一個擴展。
如果您的文件沒有擴展名保存,您將收到提示,要求應用程序使用它來打開它。如果你想告訴瀏覽器保存文件,也可以使用"Content-Disposition", "Attachment"inline將使瀏覽器嘗試直接在Word中打開文件。

string FullFilePath =//path of file //"D:\\ASP\\ASP.doc"; 
FileInfo file = new FileInfo(FullFilePath); 
if (file.Exists) 
{ 
    Response.ContentType = "application/msword"; 
    Response.AddHeader("Content-Disposition", "Attachment; filename=\"" + txtDate.Text + ".doc\""); 
    Response.AddHeader("Content-Length", file.Length.ToString()); 
    Response.TransmitFile(file.FullName); 
} 
+0

嗨,感謝它現在工作正常。 – basha

+0

很高興幫助! :-) – nunespascal

相關問題