Reputation: 4981
i have saved the pdf file to the database using file upload . now i want to retrive the pdf file from the database and it has to be linked to the linkbuttons that are dynamically created . so for each link button i have a pdf file linked to it. - how to do this in asp.net using C#
Upvotes: 0
Views: 7426
Reputation: 1039468
I would write a generic handler which will fetch the PDF from the database from a given id:
public class PdfHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int id;
if (int.TryParse(context.Request["id"], out id))
{
id = 0;
}
var connectionString = ConfigurationManager.ConnectionStrings["some_db"].ConnectionString;
using (var connection = new SqlConnection(connectionString))
using (var command = connection.CreateCommand())
{
connection.Open();
command.CommandText = "select image from some_table where image_id = :id";
command.Parameters.AddWithValue("id", id);
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
context.Response.ContentType = "application/pdf";
var cd = new ContentDisposition();
cd.FileName = "test.pdf";
cd.Inline = true;
context.Response.AddHeader("Content-Disposition", cd.ToString());
long bytesRead;
int size = 1024;
var buffer = new byte[size];
long dataIndex = 0;
while ((bytesRead = reader.GetBytes(0, dataIndex, buffer, 0, buffer.Length)) > 0)
{
var actual = new byte[bytesRead];
Buffer.BlockCopy(buffer, 0, actual, 0, (int)bytesRead);
context.Response.OutputStream.Write(actual, 0, actual.Length);
dataIndex += bytesRead;
}
}
else
{
context.Response.ContentType = "text/plain";
context.Response.Write("Not found");
context.Response.StatusCode = 404;
}
}
}
}
public bool IsReusable
{
get { return false; }
}
}
And in the aspx page just put anchors that reference this handler:
<a href="/PdfHandler.ashx?id=1">pdf 1</a>
<a href="/PdfHandler.ashx?id=2">pdf 2</a>
<a href="/PdfHandler.ashx?id=3">pdf 3</a>
...
Upvotes: 2
Reputation: 21765
First you'd have to read the records from the database.
Let's say you have the following table structure:
Id, Name, BinaryPdfData
You use ADO.NET, Linq2SQL or whatever you're using to "SELECT" Id and Name into an IEnumerable (for example a List or DataSet).
Then you bind that to an ASP Repeater where the ItemTemplate contains a LinkButton and the code behind for the Click event would then redirect you to some download page for example "downloadpdf.aspx?id={0}"
Where {0} is the Id of the record.
The download.aspx page reads the specified record from the database and puts the binary pdf data in a buffer array.
Next you'll have to set the content type etc...
I don't have time to build a good example, but you'll probably need this:
Response.Clear()
//set the content type to PDF
Response.ContentType = "application/pdf"
//add content type header
Response.AddHeader("Content-Type", "application/pdf")
//set the content disposition
Response.AddHeader("Content-Disposition", "inline;filename=helloworld.pdf")
//write the buffer with pdf file to the output
Response.BinaryWrite(Buffer)
Response.End()
Upvotes: 1