I use a similar system, but not just for PDF files.
The way I do it is to put a link to an aspx file as my table link:
private static HtmlTableRow AddDownloadFile(Downloadable dl)
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
cell.InnerHtml = "<a href=\"FileTransferDownload.aspx?file=" + dl.Id + "\" target=\"_blank\">" + dl.FileName + "</a>";
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = dl.Version.ToString();
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = dl.UploadedOn.ToString();
row.Cells.Add(cell);
return row;
}
And then fetch the file from the DB and start the download in the aspx file:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileTransferDownload.aspx.cs" Inherits="FileTransferDownload" %>
<%
string guid = Request.QueryString["file"];
string fileName = "ERROR";
byte[] data = new byte[] { 0, 0, 0, 0 };
string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DownloadDatabase"].ConnectionString;
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon))
{
con.Open();
string strcmd = "SELECT [iD] ,cn.[fileName],[description] ,[dataContent] ,[version] " +
"FROM dlContent cn " +
"WHERE cn.iD=@ID";
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(strcmd, con))
{
cmd.Parameters.AddWithValue("@ID", guid);
using (System.Data.SqlClient.SqlDataReader r = cmd.ExecuteReader())
{
if (r.Read())
{
fileName = (string) r["filename"];
data = (byte[]) r["dataContent"];
}
}
}
}
Response.Clear();
Response.AddHeader("Cache-Control", "no-cache, must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Pragma", "no-cache");
Response.AddHeader("Content-Description", "File Download");
Response.AddHeader("Content-Type", "application/force-download");
Response.AddHeader("Content-Transfer-Encoding", "binary\n");
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.BinaryWrite(data);
Response.End();
%>
The .CS file is pretty much empty:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class FileTransferDownload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}