I have a RadGrid that displays some images from a database. I'm trying to add a button to the grid that allows the user to download the image using the original file name and extension saved in the ImageName column.
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSourceImageUpload" OnItemCommand="RadGrid1_ItemCommand">
<MasterTableView DataKeyNames="ImageID" DataSourceID="SqlDataSourceImageUpload" >
<Columns>
<telerik:GridButtonColumn ButtonType="ImageButton" CommandName="download_file" UniqueName="download" ></telerik:GridButtonColumn>
<telerik:GridBoundColumn DataField="ImageName" UniqueName="ImageName" ></telerik:GridBoundColumn>
<telerik:GridBinaryImageColumn DataField="ImageData" ></telerik:GridBinaryImageColumn>
</Columns>
</MasterTableView>
<asp:SqlDataSource ID="SqlDataSourceImageUpload" runat="server"
ConnectionString="<%$ ConnectionStrings:123ConnectionString %>"
SelectCommand="SELECT * FROM [tbl_Images]"
DeleteCommand="DELETE FROM [tbl_Images] WHERE [ImageID] = @ImageID"
InsertCommand="INSERT INTO [tbl_Images] ([ImageData], [ImageName], [Text])
VALUES (@ImageData, @ImageName, @Text)"
UpdateCommand="UPDATE [tbl_Images] SET [Text] = @Text WHERE [ImageID] = @ImageID">
<DeleteParameters>
<asp:Parameter Name="ImageID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:QueryStringParameter Name="IncidentID" QueryStringField="ID" Type="String" />
<asp:Parameter Name="Text" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Text" Type="String" />
<asp:Parameter Name="ImageID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
I found this code online, and honestly not sure how to make it all work with what I'm trying to do. I really feel like I'm over complicating this whole thing. Is there an easy button I'm missing somewhere?
The ultimate goal is for the user to click a download button and have the image download using the ImageName field as the file name.
Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
If e.CommandName = "download_file" Then
Dim ditem As GridDataItem = CType(e.Item, GridDataItem)
Dim filename As String = ditem("ImageName").Text
Dim path As String = MapPath("/sample/" & filename)
Dim bts As Byte() = System.IO.File.ReadAllBytes(path)
Response.Clear()
Response.ClearHeaders()
Response.AddHeader("Content-Type", "Application/octet-stream")
Response.AddHeader("Content-Length", bts.Length.ToString())
Response.AddHeader("Content-Disposition", "attachment; filename=" & filename)
Response.BinaryWrite(bts)
Response.Flush()
Response.[End]()
End If
End Sub