0

I have a sql server database that stores txt files in a varbinary column. I have a gridview controller (C#) that lists the files that are stored. What I would like to have happen is this: if a two or more files are selected from the gridview, the app or the database (doesnt matter to me) will concatentate all of the selected files content into one file that the user can download.

so if file one is a text file that contains the word "Hello" and File two is a text file the contains the word "world", the application will read content from both and create a new test file that contains "Hello World"

all suggestions welcome.

user2219930
  • 125
  • 17
  • I think you are asking how to read data from your database and then combine it all into a single file. If that is correct, you can google most of this and come back and ask us specific questions about your implementation. – user1477388 Feb 21 '14 at 18:20
  • This is just a requirement. Where is the code you tried? – Ananthan Unni Feb 21 '14 at 18:22
  • I have no idea what to try. this is not text in the database. these are binary files. so the files need to be opened, the data needs to be read out of them, then the data needs to be combined into one file. this is a legacy system and this is a new requirement that i dont know how to handle. If it were up to me, i would just store the file text from the start – user2219930 Feb 21 '14 at 18:37

2 Answers2

0

One way you could accomplish this is by having the query concatenate the data for you and create the file from that.

Such as,

select convert(varchar(1024), varbinaryA) + convert(varchar(1024), varbinaryB) from A_Table

George
  • 16
0

Your subject reads "two binary files" but the example you gave was "two text files". Those are two completely different things. Concatenating text files is easy and straightforward. You can use something like:

string firstFile = System.IO.File.ReadAllText("myfile1.txt");
string secondFile = System.IO.File.ReadAllText("myfile2.txt");

and then return the concatenated result firstFile + " " + secondFile to the user.

However, for binary files, if you concatenate them, they will no longer work. For example, the concatenation of firstFile.exe and secondFile.dll is not useful for anything. If you want to offer your users a single download for binary files, I suggest adding them to one compressed file (e.g. Zip file) and offer that file as a download. Your users can then extract the files and use them.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
  • I think this will work. I will test it tomorrow. The confusion between text file and binary is this: the file exist on the users computer as a text file, then when they upload it, it is converted to varbinary. so as it is stored in the database, it is not a text file. it is not stored on the server file system. thus, the data, needs to be converted back from binary to text, then concatenated. and the concatenated file needs to be downloaded, not the individual files. – user2219930 Feb 21 '14 at 23:17
  • 1
    In this case you will have to read the varbinary fields into a memory steam and depending on what you want, either a) use the streams to write a new single text file and return it in the response; or b) use the streams to create the text files in one zip file and return it in the response. Of course, you can always create temporary files on the server from the varbinary and then read them using System.IO.File like I mentioned, but using a memory stream in your case is far more efficient. – Racil Hilan Mar 06 '14 at 08:05