I have a SQL Server table with filestream enabled. I am able to upload files here and update as well. How do I read these files into an .aspx web page? The files are .pdf
files.
There are 2 files are associated with each row.
I am reading the row into a .aspx page and at the bottom I want the files to open up one below the other. This is for the user to print the row with the files.
I have checked out the code suggested by @dinglemeyer. I do want to conver my pdf to image. I am assuming there is a simple way:
<asp:GridView ID="gridview_f" runat="server" ShowHeaderWhenEmpty="false" PageSize="2" DataKeyNames="ID" DataSourceID="sql_files" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="selID" runat="server" Text='<%# Bind("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="selFname" runat="server" Text='<%# Bind("filename") %>' />
<br />
<asp:image ID="selFile" runat="server" ImageUrl='<%# "Handler.ashx?id="+Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
// code behind for ginfing the grid as the parentId is extracted from other form values.
private void gridView_Data(string id)
{
sql_files.SelectCommand = "SELECT ID, filename from myFILESTABLE where PARENTID=" + id;
}
// Handler.ascx
public void ProcessRequest(HttpContext context)
{
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataReader sdr = null;
conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["my_dbconn"].ConnectionString);
try
{
cmd = new SqlCommand("SELECT filecontent from myFILESTABLE WHERE ID=" + context.Request.QueryString["ID"], conn);
conn.Open();
sdr = cmd.ExecuteReader();
while (sdr.Read())
{
context.Response.ContentType = "content/pdf";
context.Response.BinaryWrite((byte[])sdr["filecontent"]);
}
sdr.Close();
}
finally
{
conn.Close();
}