我工作的一個網站,我繼承了一個與ASP.Net修建,這我只是稍微熟悉。其中一個頁面允許鏈接到文檔(單詞或pdf),當點擊該文檔時,提示用戶保存或打開文件,但不顯示文件的真實路徑。通過這種方式,它可以防止用戶將url粘貼到文件 - 該鏈接指向檢查有效會話的aspx文件,然後檢索文件。HTM內容在ASP.Net
無論如何,因爲有很多遺留代碼,我還需要使用一堆靜態htm文件來做到這一點,但是需要顯示這些文件而不是提示用戶保存它們,這就是現在發生的情況。我嘗試將內容類型更改爲應用程序/文本,應用程序/ html,text/html等,但沒有奏效,然後嘗試添加內容處置響應標頭。當我這樣做,構建,並嘗試鏈接到文件,我得到了幾個運行時異常:
[FormatException: Input string was not in a correct format.]
Microsoft.VisualBasic.CompilerServices.Conversions.ParseDecimal(String Value, NumberFormatInfo NumberFormat) +206
Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value) +110
[InvalidCastException: Conversion from string "inline; filename=\" + myFile + " to type 'Long' is not valid.]
Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value) +428
cmswebasp.CMSModdoclinks.DownloadFile(String file) +1704
cmswebasp.CMSModdoclinks.Page_Load(Object sender, EventArgs e) +625
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
下面是代碼的頁面片段:
Response.AddHeader("Content-Disposition", "inline; filename=" & file)
Dim fi As New FileInfo(myFile)
Response.AddHeader("Content-Length", fi.Length)
Dim contentType As String = ""
Dim fileExt As String = file.Split(".")(1)
Select Case fileExt
Case "htm"
contentType = "application/text"
Case Else
contentType = "application/octet-stream"
End Select
Response.ContentType = contentType
Response.WriteFile(myFile)
我必須做什麼與一個htmlwriter對象或東西?我不能只是打開一個新的瀏覽器窗口並顯示文件,或者是否必須提示用戶使用這種方式?
對不起,終於實現了這一點,它的工作原理就像一個魅力 - 我使用http://www.dotnetperls.com/ashx的代碼爲了提供正確的文件名到處理程序。 – 2012-05-04 19:28:24
但是,我唯一的問題是,即使我正在執行IRequiresSessionState,如上所示,我可以訪問URL並因此無需登錄即可訪問該文件...任何想法爲什麼可能是這樣? – 2012-05-04 19:30:09
沒關係,我想我只需要編寫條件代碼,如果他們的會話變量沒有設置,就會阻止用戶訪問頁面。 – 2012-05-04 19:55:06