简体   繁体   中英

Why ASP vb page redirecting to a ASPX VB.net page to download a file does not work

I have an ASP page with a link inside of it directing it to a VB.net ASPX page which sends a file to the client. The downloaded file says unsupported file type when clicked on after downloading.

I have an ASPX page which does the EXACT same thing (a link to that page) and the file downloads perfectly.

I want to note that I am using "SecureWeb" browser on a phone and that Chrome works fine from both pages, so maybe I am missing something required. It seems to me like this browser is ignoring the header mime type or something (It is correctly being set to msword, and the file is at the location specified).

Code:

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

I know this basically doesn't show up in the orig question because it didn't even cross my mind to pertain to it at ALL, but I thought I would add the answer I came across in case it helped anyone.

I have NO clue why, but this line was causing the issue: (Line 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

Although the code was correctly skipping the first block of code, it was somehow causing the response to send a corrupt file (if someone can shed any light on this that'd be great), but I don't see what this has to do with it since it continued to the 2nd block of the else statement.

I'm Referring to the Request.Browser.IsMobileDevice . Since this didn't cause an issue in Chrome, I'm thinking there was some type of security inside of the browser being activated to not allow the file. It would make sense since this is Webroot SecureWeb browser on these phones.

I appreciate any time anyone spent on this issue. Very frustrating since it was in the last place you would look.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM