basit0079 says:
Response.Clear()
Response.Buffer =
True
Response.Charset =
""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.AppendHeader(
"Content-Disposition"
, Convert.ToString(
"attachment; filename="
) & fileName)
Response.Flush()
Response.[
End
]()
Replace the above with the below code.
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = ContentType
Response.AppendHeader("Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(fileName))
Response.WriteFile(fileName)
Response.Flush()
Response.[End]()
Below is the complete sample code.
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)), New DataColumn("ImagePath", GetType(String))})
dt.Rows.Add(1, "Chrysanthemum")
dt.Rows.Add(2, "Koala")
dt.Rows.Add(3, "Tulips")
dt.Rows.Add(4, "Jellyfish")
dl_Options.DataSource = dt
dl_Options.DataBind()
End If
Me.RegisterPostBackControl()
End Sub
Private Sub RegisterPostBackControl()
For Each row As DataListItem In dl_Options.Items
Dim lnkFull As LinkButton = TryCast(row.FindControl("LinkButton1"), LinkButton)
ScriptManager.GetCurrent(Me).RegisterPostBackControl(lnkFull)
Next
End Sub
Protected Sub DownloadFile(sender As Object, e As EventArgs)
Dim id As Integer = Integer.Parse(TryCast(sender, LinkButton).CommandArgument)
Dim fileName As String = Nothing
Dim ContentType As String = Nothing
Dim constr As String = ConfigurationManager.ConnectionStrings("RegistrationConnectionString").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand()
cmd.CommandText = "SELECT TOP 4 * FROM Files where Id = @Id"
cmd.Parameters.AddWithValue("@Id", id)
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
sdr.Read()
fileName = "D:\Documents\upload\" + sdr("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=" + System.IO.Path.GetFileName(fileName))
Response.WriteFile(fileName)
Response.Flush()
Response.[End]()
End Sub
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id", typeof(int)),
new DataColumn("ImagePath", typeof(string)) });
dt.Rows.Add(1, "Chrysanthemum");
dt.Rows.Add(2, "Koala");
dt.Rows.Add(3, "Tulips");
dt.Rows.Add(4, "Jellyfish");
dl_Options.DataSource = dt;
dl_Options.DataBind();
}
this.RegisterPostBackControl();
}
private void RegisterPostBackControl()
{
foreach (DataListItem row in dl_Options.Items)
{
LinkButton lnkFull = row.FindControl("LinkButton1") as LinkButton;
ScriptManager.GetCurrent(this).RegisterPostBackControl(lnkFull);
}
}
protected void DownloadFile(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
string fileName = null;
string contentType = null;
string constr = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT TOP 4 * FROM Files where Id=@Id";
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
fileName = "D:\\Documents\\upload\\" + sdr["Name"].ToString();
}
con.Close();
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(fileName));
Response.WriteFile(fileName);
Response.Flush();
Response.End();
}
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Panel runat="server" ID="Panel1" ChildrenAsTriggers="false">
<div style="vertical-align: middle; min-height: 480px;" class="pre-scrollable">
<div>
<asp:DataList ID="dl_Options" runat="server" p Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center"
RepeatLayout="Table">
<ItemTemplate>
<div>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("ImagePath") %>'></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("ImagePath") %>' CommandArgument='<%# Eval("Id")%>'
OnClick="DownloadFile"></asp:LinkButton>
</div>
</ItemTemplate>
</asp:DataList>
</div>
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>