I am creating a web app. For this purpose I use Microsoft SQL Server 2016 (SP1-GDR) (KB4505219) - 13.0.4259.0 (X64) and ASP.NET MVC 5.
In my database I have a table with PDF files in it. I have stored the file content as blob in that table. I have also stored the filename in that table.
Now I want to give my clients the possibility to download multiple PDF files. The PDF files should be concatenated. So if the client requests for all PDF files, he or she should get all PDF files concatenated to one single PDF file.
I thought about two ways to concatenate the PDF files.
- First is to concatenate the PDF files in the SQL Server query. So when I get the blob from the database it is already concatenated and I do not have to do anything further in my C# code.
- Second is to get all blobs from the database and concatenate the blobs in C#.
I have tried to use the COALESCE
function in SQL Server to concatenate the blobs in SQL Server.
DECLARE @blobcontent varbinary(max);
SELECT
@blobcontent = COALESCE(@blobcontent, 0x0) + FileTable.fileContent
FROM
FileTable;
SELECT @blobcontent;
This did not work for me.
So then I tried to concatenate the blob in C# after getting all blobs from the database. I tried to create a new byte-array variable and put all blobs together into that new byte-array.
...
var file1 = GetFileById(1); /*the function GetFileById(int id) returns a tuple (byte[], string) with the file-content as the first Element and the file-name as the second element*/
var file2 = GetFileById(2);
var concatenatedFile = file1.Item1.Concat(file2.Item1).ToArray();
...
This did also not work for me.
Is there a smart way to solve this problem?