2013-12-16 16 views
0

我試圖從數據庫下載word文檔(.docx)。使用Response.BinaryWrite(MyByte)和Context.ApplicationInstance.CompleteRequest()時獲取不需要的輸出()

所以我用下面的代碼下載功能

Response.BinaryWrite(MyByte) 
Context.ApplicationInstance.CompleteRequest() 

末不過這樣它拋出的錯誤,而文件的每次打開不需要的內容寫入我的Word文件。

的文件已損壞,無法打開

我GOOGLE了這個問題,我得到了使用到Response.End()的代替Context.ApplicationInstance.CompleteRequest()

但是,由於線程被中止,Response.End拋出錯誤。並完美打開文件。

+0

最後我得到了這個解決方案dding Response.AddHeader(「content-length」,mytBytes.Length.ToString()) –

回答

0

我已經試過這個解決我的問題

我只是設法解決這個問題通過

Response.ClearContent(); 
Response.ClearHeaders(); 

所以整個事情看起來像更換

Response.Clear(); 

byte[] downloadBytes = doc.GetData(); 
Response.ClearContent(); 
Response.ClearHeaders(); 

Response.Buffer = true; 
Response.ContentType = "application/msword"; 
Response.AddHeader("Content-Length", downloadBytes.Length.ToString()); 
Response.AddHeader("Content-Disposition", "attachment; filename=myFile.docx"); 
Response.BinaryWrite(downloadBytes); 
Response.Flush(); 
Response.End();