Concatenate two PDF files saved as a blob - c#

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?

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.

Related

Azure Data Lake: How to get Processed files

I've just started working with Data Lake and I'm currently trying to figure out the real workflow steps and how to automatize the whole process.
Say I have some files as an input and I would like to process them and download output files in order to push into my data warehouse or/and SSAS.
I've found absolutely lovely API and it's all good but I can't find a way to get all the file names in a directory to get them downloaded further.
Please correct my thoughts regarding workflow. Is there another, more elegant way to automatically get all the processed data (outputs) into a storage (like conventional SQL Server, SSAS, data warehouse and etc)?
If you have a working solution based on Data Lake, please describe the workflow (from "raw" files to reports for end-users) with a few words.
here is my example of NET Core application
using Microsoft.Azure.DataLake.Store;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest.Azure.Authentication;
var creds = new ClientCredential(ApplicationId, Secret);
var clientCreds = ApplicationTokenProvider.LoginSilentAsync(Tenant, creds).GetAwaiter().GetResult();
var client = AdlsClient.CreateClient("myfirstdatalakeservice.azuredatalakestore.net", clientCreds);
var result = client.GetDirectoryEntry("/mynewfolder", UserGroupRepresentation.ObjectID);
Say I have some files as an input and I would like to process them and download output files in order to push into my data warehouse or/and SSAS.
If you want to download the files from the folder in the azure datalake to the local path, you could use the following code to do that.
client.BulkDownload("/mynewfolder", #"D:\Tom\xx"); //local path
But based on my understanding, you could use the azure datafactory to push your data from datalake store to azure storage blob or azure file storge.

Upload a text file with Azure Mobile Services

I want to create an application where the user after authentication can upload a file(txt/xml etc.) using the Azure Mobile Services and after that he can download only those files which were uploaded by himself.
I've watched a lot of tutorials (including this one: link ) but in this case they simply inserts a row to a database table. I want basically the same thing, just with files. How can I do that?
I'm really new to this, so I'm just guessing, but should I upload the files to Blob Storage, and store a link in the database pointing to that file? I'm searching for the best practice.
Yes, you are correct!
You would be limited in size if you tried to store the text file as a field in the database.
http://azure.microsoft.com/en-us/documentation/articles/mobile-services-windows-store-dotnet-upload-data-blob-storage/
Shows how to do what you want to do but with images.
You would want to change the image stream to a text stream here:
// Get the new image as a stream.
using (var fileStream = await media.OpenStreamForReadAsync())
{
...
}
And use the Stream classes instead to open the file stream:
http://msdn.microsoft.com/en-us/library/windows/apps/system.io.stream(v=vs.105).aspx

Store and retrieve VSD file into SQL FILETABLE

How to save a .VSD file into a FileTable in SQL Server 2012? And also need to retrieve and display the .VSD file on a web page?
Filetable column : file_stream varbinary(max)
Any help? Thank you.
Code used:
sfileName = "sample.vsd"
Dim fs As New FileStream(sfileName, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(CInt(fs.Length))
VARBINARY(MAX) would be the correct data type to store binary data and you will want to use the FILESTREAM attribute. If you assume that this is the only type of file you will be storing in your table, then you are good. If you need to store multiple types of files you will want to add another column to capture the file type and then have your application display it correctly.
Check out the following post that should give you a better understanding. Article mentions SQL 2008 but it is applicable to 2012 and 2014:
http://blogs.technet.com/b/dataplatforminsider/archive/2008/04/14/sql-server-2008-makes-it-easy-to-manage-blobs-and-files.aspx
I have not found a way to save and retrieve the Visio (.vsd) file from the FILETABLE in SQL server 2012.
I tried to save the Visio file to the latest version of Visio (.vsdx) but it didn't work.
The work around for it is save the .vsd file to PDF or Image file. Then save the file to SQL Server FILETABLE using the code mentioned in my question.

importing data from csv file in remote machine to postgresql table

How can i use copy command for bulk insertion of data into postgresql table from csv file present in remote machine using C#?
I've a front end in C# using which i need to load data into postgresql table from csv file. The database is in remote server and the csv file is in my machine.
Assuming the CSV file is on the machine running the C# program, you need to use COPY ... FROM STDIN. This is difficult to use directly from client drivers, but most of them support their own interfaces on top.
I'm guessing you are using nPgSQL since you didn't bother to mention what client you're using. If so, you can use NpgsqlCopyIn.
Not possible at all, unless you mount the remote machine on the postgres server. To copy postgres needs to be able to access the file locally.
You could try to scp the file from A to B, or parse the file yourself and do bulk inserts into postgres, ie:
create table file (structure of the file);
Read 100 lines
insert into tabe file
values (line 1)
,(line 2)
...
,line (100)

how to do attachments for each sql record in c#

I am developing a desktop application in c# & sql 2005. With candidate data entry form I want to provide option to attach required documents(in pdf format) wid data. Kindly let me know the best method. Thank you in advance.
Simply create a table that will contain the filename and server path for the file to be attached, then create a method to copy the attached file to the server location and store the relivant information (name and path) in the table. Use other methods to retrive the file from the server location when requested. Simple.
I personaly prefer to store the documents as BLOBs since server file structures and paths can change over time.
Well then unfortunately you have to either manage the file storage yourself using the servers file system, or you could store it in the db itself (IT WILL GET BLOATED!!!)
See sql server 2005 file storage
How To: Encrypt and Manage Documents with SQL Server 2005
OK, then for file management
see this example
File Manager Component

Categories

Resources