[英]Why ASP vb page redirecting to a ASPX VB.net page to download a file does not work
我有一個ASP頁,其中包含一個鏈接,該鏈接將其定向到將文件發送到客戶端的VB.net ASPX頁。 下載后單擊時,下載的文件顯示不支持的文件類型。
我有一個ASPX頁面,該頁面執行的操作完全相同(指向該頁面的鏈接),並且文件下載完美。
我想指出的是,我在手機上使用“ SecureWeb”瀏覽器,並且Chrome在兩個頁面上都能正常工作,因此也許我缺少了某些必需的東西。 在我看來,此瀏覽器似乎忽略了標題的mime類型或其他內容(已將其正確設置為msword,並且文件位於指定的位置)。
碼:
If User.Identity.IsAuthenticated Then
Func.LogMyActivity()
Dim FileDetails As MyFileDetails= Func.getFileDetails(Request.QueryString("id"))
If FileDetails.FilePID = 0 Or Func.checkUserFile(Session("user_id"), Request.QueryString("id")) <> "" Then
Dim FileToDownload As String = Func.getFileName(Request.QueryString("id"))
Dim ServerPath As String = Server.MapPath("~/FileLoc/" & FileToDownload)
Dim file As System.IO.FileInfo = New System.IO.FileInfo(ServerPath)
If file.Exists Then
Response.Clear()
Dim WebPath As String = System.Configuration.ConfigurationManager.AppSettings("FileLoc") & Trim(FileToDownload)
Dim FileName As String = System.IO.Path.GetFileName(WebPath)
Dim FileExt As String = System.IO.Path.GetExtension(FileName).ToLower()
Dim Directory As String = System.IO.Path.GetDirectoryName(ServerPath)
Select Case Right(FileExt, 3)
Case Is = "doc", "dot"
Response.ContentType = "application/msword"
Case Is = "xls"
Response.ContentType = "application/vnd.ms-excel"
Case Is = "ppt", "pot"
Response.ContentType = "application/vnd.ms-powerpoint"
Case Is = "pdf"
Response.ContentType = "application/pdf"
Case Is = "pps"
Response.ContentType = "application/vnd.ms-powerpoint"
Case Is = "mp3"
Response.ContentType = "audio/mpeg"
Case Is = "txt"
Response.ContentType = "text/plain"
Case Else
Response.ContentType = "text/plain"
End Select
Response.AddHeader("Content-Disposition", "attachment; filename=" & FileToDownload & "")
Response.AddHeader("Content-Length", file.Length.ToString())
Response.WriteFile(file.FullName, True)
Response.End()
Else
MessageLabel.Text = "Sorry that file cannot be located. Please contact your administrator."
End If
Else
MessageLabel.Text = "Failed"
End If
Else
Response.Redirect("listfiles.aspx")
End If
我知道這個問題基本上不會出現在orig問題中,因為它甚至都沒有讓我想到與之相關的所有問題,但是我想我會添加我遇到的答案,以防它對任何人都有幫助。
我不知道為什么,但是這是引起問題的原因:(第1行)
If Request.querystring("Inc") <> "" and Request.Browser.IsMobileDevice = False Then
....
else
Response.AddHeader("Content-Disposition", "attachment; filename=" & FileToDownload & "")
Response.AddHeader("Content-Length", file.Length.ToString())
Response.WriteFile(file.FullName, True)
Response.End()
end if
盡管代碼正確地跳過了第一段代碼,但是它卻以某種方式導致響應發送了損壞的文件(如果有人可以對此有所了解的話),但是我不知道該怎么做因為它繼續到else語句的第二個塊。
我指的是Request.Browser.IsMobileDevice
。 由於這不會在Chrome中引起問題,因此我認為正在激活瀏覽器內部某種類型的安全性以不允許該文件。 這將是有道理的,因為這是這些電話上的Webroot SecureWeb瀏覽器。
感謝任何人在此問題上花費的任何時間。 非常令人沮喪,因為它位於您最后要看的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.