Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having trouble downloading a file from a server with a dynamic link button

Status
Not open for further replies.

mdProgrammer

Programmer
Oct 2, 2004
71
0
0
US
I have a datagrid that creates a dynamic linkbutton control if the column contains an attachment field. This linkbutton should download the file, but doesn't (The popup comes up, but then says unable to download file). I get different results on 3 different browsers.

Ok, first, I have a function (found on the Internet) that downloads a file from the database just fine if I use a standard button on the webform. This works as expected. As a test, I made a debug page to run this function with the webform button control, and from one that adds a dynamic linkbutton. Here's what happens -

IE (IE9) - Simply says "...couldn't be downloaded" (note - the latest version of IE isn't used for compatibility with other 3rd party software)
Google Chrone - When I first tested, it said there was a Network Error. Subsequent tests will download the file. So, to me, it's intermittent.
FireFox - Will download the file.

(IE is needed for modal popup functionality and accessibility, so just using Chrome and Firefox isn't an option.)

This is a barebones code test that I did -

Code:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Attachment.aspx.vb" Inherits="WebApp.Attachment" %>

<!DOCTYPE html>

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
      <asp:Button ID="Button1" runat="server" Text="Button" />
    
    </div>
    </form>
</body>
</html>

Code:
Imports System.Data.SqlClient

Public Class Attachment
  Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    ' For testing - add link button to download file (will be in gridview)
    Dim lnk As New LinkButton
    lnk.ID = "lnkTest" ' Actual code builds the ID based on Attachment_ID
    lnk.Text = "Test"
    lnk.CommandArgument = 30 ' Hardcoded for testing 
    AddHandler lnk.Click, AddressOf DownloadFile
    form1.Controls.Add(lnk)

  End Sub

  Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' This is from the webform
    DownloadFile()
  End Sub

  Dim db As New dbData

  ' Commented out the command arguments to make sure that wasn't the cause
  ' The dynamic control calls this function
  Protected Sub DownloadFile() 'sender As Object, e As EventArgs)
    Dim id As Integer = 30 'Integer.Parse(TryCast(sender, LinkButton).CommandArgument)
    Dim bytes As Byte()
    Dim fileName As String, contentType As String
    Dim constr As String = db.AttachmentConnection.ConnectionString
    Using con As New SqlConnection(constr)
      Using cmd As New SqlCommand()
        cmd.CommandText = "select Attachment_File_Name, Attachment_Content, Attachment_File_Type from buAttachment where Attachment_ID=@Id"
        cmd.Parameters.AddWithValue("@Id", id)
        cmd.Connection = con
        con.Open()
        Using sdr As SqlDataReader = cmd.ExecuteReader()
          sdr.Read()
          bytes = DirectCast(sdr("Attachment_Content"), Byte())
          contentType = sdr("Attachment_File_Type").ToString()
          fileName = sdr("Attachment_File_Name").ToString()
        End Using
        con.Close()
      End Using
    End Using
    Response.Clear()
    Response.Buffer = True
    Response.Charset = ""
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ContentType = contentType
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName)
    Response.BinaryWrite(bytes)
    Response.Flush()
    Response.End()
  End Sub

End Class
 
Thanks, I'll keep those links in mind. The above code seems to work in IE10+ (which most of our users have - some still use IE9 for older legacy web applications on outside networks). There's also the work-around of saving the file first, which can then open.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top