簡體   English   中英

ASP.NET問題IE10打開存儲在SQL Server DB中的文件

[英]ASP.NET issue wih IE10 opening files stored in sql server DB

我有一個小的asp.net應用程序,它允許用戶上傳附件,並通過asp:linkbutton從另一個屏幕下載它們。 在IE10中,當我單擊鏈接按鈕以下載上載的文件時,IR會顯示打開,保存,取消對話框,但會顯示URL代替文件名。 在Safari和Firefox中,這不會發生。 它讓我發瘋。 我嘗試了所有的標題/內容類型,內容配置組合,但都沒有碰到運氣。 下面是我的代碼片段,可在Firefox和Safari中使用-它將二進制文件簡單地寫入響應中。

Dim bytes() As Byte = CType(dt.Rows(0)("UploadedFiles"), Byte())
Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
Response.AddHeader("Content-Disposition", "attachment;filename=" & Chr(34) & 
dt.Rows(0)    ("FileName").ToString & Chr(34))
Response.Buffer = True
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = dt.Rows(0)("ContentType").ToString()
Response.BinaryWrite(bytes)
Response.Flush()
Response.Close()
Response.End()

這就是我的方法,它可以在IE8中使用,很抱歉,我沒有安裝IE10進行測試:

// myFile = an object containing information about the file being served
Response.Clear()
Response.Cache.SetExpires(DateTime.Now.AddMonths(1))
Response.Cache.SetCacheability(HttpCacheability.Public)
Response.ContentType = myFile.contentType
Response.AddHeader("Content-Disposition",  "attachment; filename=" & myFile.targetFilename)
Response.BinaryWrite(bytes) // your code
//Response.WriteFile(myFile.sourceFilename, True) // my code
Response.End()

緩存的內容可能對您來說不是必需的。

我使用Response.WriteFile是因為我將文件存儲在文件系統中,而不是存儲在數據庫/字節數組中。 您也許可以直接掛斷對BinaryWrite

唯一的不同是您在Content-Disposition標頭中的文件名兩邊加上了雙引號。 我知道rfc2616向他們展示了,但是我的代碼在沒有它們的情況下也可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM