I have a requirement where I need to upload some PDF files from an ASP.NET page. This upload action is done by admin user. These files which are selected using individua开发者_开发问答l fileupload controls should merge and get stored in a SQL Server database. Now the normal user should be able to login to an asp.net page where a link should be displayed to open the file stored in the database and view it using adobe reader.
I see the following challenges:
If I select multiple pdf files, how can I store it in the database in a merged format.
Since the file is stored in the database how can I create a link and point the file stored in the database
First of all, is this doable. I know about storing a file in a database, but how can I merge and store a file in a database?
iTextSharp is a free PDF library that will allow you to merge the PDFs. Here's a code sample doing just that.
Retrieving the file from SQL server is a simple matter. This discussion thread has an example.
Code from thread:
byte[] data = TheMethodToReadTheFieldFromDB();
using (Stream st = new MemoryStream(data))
{
long dataLengthToRead = st.Length;
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + theFileName + "\"");
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
Int32 lengthRead = st.Read(buffer, 0, blockSize);
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Flush();
Response.Close();
}
Response.End();
Merging PDF files is a task that needs to be handled by a PDF manipulation/image processing library. For example Atalasoft DotImage. There may be free alternatives too.
To link to a file in the database, you'd need to create a handler or page (e.g. .ashx HttpHandler) which, given an id in a query string for example, will transmit the page to the client. You'll need to set the correct MIME type on the response.
精彩评论