Link to home
Create AccountLog in
Avatar of IT-CICA
IT-CICAFlag for Canada

asked on

file download giving error in ASP.NET site

Hi,

We have a website on which, clicking a hyperlink should display a download dialog box to the user.

It used to work fine. Now suddenly it has stopped working in IE. It works fine in all other borsers (firefox, safari etc)

Following is the code in the hyperlink click event:

Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
        Dim strFilePath As String = ""
        Dim strFileName As String = ""

        strFileName = "JPGColour.zip"
        strFilePath = "English\JPG.zip"
     
        Server.UrlEncode(strFilePath)
        Response.Clear()
        Response.ContentType = "application/octet-stream"
        If Request.Browser.Type.ToString.Contains("Firefox") Then
            Response.AddHeader("Content-Disposition", _
          "attachment; filename=""" & strFileName & "")
        Else
            Response.AddHeader("Content-Disposition", _
          "attachment; filename=" & strFileName)
        End If

        Response.Flush()
        Response.TransmitFile(strFilePath)

-------------------------------------------------------------------------------------------------------

The error message says:

Unable to download

Unable to open this internet site. The requested site is either unavailable or can not be found. Please try again later.
--------------------------------------------------------------------------------------------------
Any work around for this?

Thanks in advance.
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

The path and file and do not match because they are different:
  strFileName = "JPGColour.zip"
        strFilePath = "English\JPG.zip"
it should be:
  strFileName = "JPGColour.zip"
  strFilePath = Server.MapPath("~/English/JPGColour.zip")

Also replace these lines:
Response.Flush()
        Response.TransmitFile(strFilePath)
with
Response.TransmitFile(strFilePath)
Response.End()

Check:
http://csharpdotnetfreak.blogspot.com/2012/01/download-file-from-server-in-aspnet.html
Avatar of IT-CICA

ASKER

The above code doesn't work. It displays a dialog box to download the asp page on which this code exists.

The code from my original post works fine on test server in IE.

The only difference between test and live is that LIVE is a secure site.
ASKER CERTIFIED SOLUTION
Avatar of IT-CICA
IT-CICA
Flag of Canada image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of IT-CICA

ASKER

I am able to download the file now.