2011-03-08 60 views
0

當我嘗試下載存儲在SQL DB中的文件時,我只對Firefox有一個非常奇怪的問題(適用於IE瀏覽器& Chrome)。僅當用戶嘗試將文件保存在其計算機上時纔會出現問題,因爲它無法識別文件的擴展名。如果用戶試圖打開它並且瀏覽器能夠檢測到它是否爲word,excel或pdf文件,那麼它工作正常。這裏是我的代碼塊:C#文件通過https與Firefox問題下載https

Attachments attach = AttachmentsSession[e.Item.ItemIndex] as Attachments; 
string extension = attach.Extension; 
byte[] bytFile = attach.AttachmentData; 
string fileName = attach.Name; 

Response.ClearHeaders(); 
Response.Clear(); 
Response.Buffer = true; 

if (extension == ".doc") 
{ 
    Response.ContentType = "application/vnd.ms-word"; 
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName); 
} 

else if (extension == ".docx") 
{ 
    Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; 
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName); 
} 

Response.Charset = ""; 
Response.BinaryWrite(bytFile); 
HttpContext.Current.ApplicationInstance.CompleteRequest(); 
Response.End(); 

回答

2

我不能ProNeticas'發佈評論,所以:

「應用程序/字」 不是公認的MIME類型,至少所有的.docx,我懷疑瀏覽器會知道如何處理它。

的.DOCX正確的MIME類型爲application/vnd.openxmlformats-officedocument.wordprocessingml.document,並且他已經有MIME類型是.DOC

正確見Office Mime Types

+1

+1我們不得不用Firefox這個確切問題,XLSX文件。它歸結爲必須發送正確的MIME類型 – NotMe 2011-03-08 18:59:42

+0

+1,看看在這裏接受的MIME類型: http://www.w3schools.com/media/media_mimeref.asp – Justin 2011-03-08 19:01:43

0

嘗試......

Response.AddHeader('Content-type', 'application/msword'); 
Response.AddHeader('Content-Disposition', 'attachment; filename="file.docx"');