0

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ali
  • 410
  • 5
  • 21
  • 4
    Both the ways will not work, as concatenation of two files will not make a single file (It will be corrupted). You need to do this at your application server only by creating entirely a new file using some libraries like itextsharp or aspose or you can create a single zip file containing all the files. – PSK Jan 10 '21 at 10:54
  • Does this answer your question? [How to merge multiple pdf files (generated in run time)?](https://stackoverflow.com/questions/15925616/how-to-merge-multiple-pdf-files-generated-in-run-time) – Achtung Jan 10 '21 at 10:54
  • No it did not @Achtung because I am not using iTextSharp – Ali Jan 10 '21 at 10:57
  • @PSK thats what I thought... You have confirmed my suspicions. Thank you. – Ali Jan 10 '21 at 10:58
  • That is one tool that can be used. If you do not want to use that select another. But you need a tool. – Achtung Jan 10 '21 at 11:05
  • Perhaps a different approach - generate a list of individual files that the user could selectively download. – Peter Smith Jan 10 '21 at 11:12
  • @Ali PDF is a binary format. You can't concatenate binary files and get back a valid file. Even with text files, concatenating eg XML or JSON files will produce an invalid file – Panagiotis Kanavos Jan 10 '21 at 11:13
  • I was just thinking that somehow there is a way without using a tool. But there obviously isn't. So the client just gets the PDFs as a zip. Thanks for your help. – Ali Jan 10 '21 at 11:53
  • Someone pursuing a Masters degree should be well aware that comments like "does not work" are not meaningful. Here the issue is obvious so no qualification is needed. But generally, you need to provide context and useful information about any problems you encounter. Develop good habits. I also suggest you really think about how you as a user expect to use / work with a set of downloaded PDF files. Perhaps the user wants those separate files and not one giant one? Don't assume and push more work on the user. – SMor Jan 10 '21 at 13:28

1 Answers1

0

Ok. As I also suspected, it is not possible to concatenate blobs in this form (see comment by PSK). I will solve this problem by simply providing the client the files as zip, because I don't want to include any other libraries for this.

Ali
  • 410
  • 5
  • 21