开发者

Storing, uploading and downloading files in ASP.NET and SQL Server database

开发者 https://www.devze.com 2023-03-27 00:56 出处:网络
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 con

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:

  1. If I select multiple pdf files, how can I store it in the database in a merged format.

  2. 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?


  1. iTextSharp is a free PDF library that will allow you to merge the PDFs. Here's a code sample doing just that.

  2. 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();


  1. 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.

  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消