INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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="http://www.w3.org/1999/xhtml">
<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 

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

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close