为什么ASP vb页面重定向到ASPX VB.net页面下载文件不起作用

时间:2015-03-17 12:48:18

标签: asp.net vb.net

我有一个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

1 个答案:

答案 0 :(得分:0)

我知道这基本上没有出现在原始问题中,因为它甚至没有让我全神贯注于它,但我想我会添加我遇到的答案,以防它帮助任何人。< / p>

我不知道为什么,但这一行造成了问题:(第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浏览器。

我很感激任何人在这个问题上花的时间。非常令人沮丧,因为它是在你看的最后一个地方。