Friday, February 17, 2012

Extracting Binary data from SQL DB to a location on the web server

Hey all,

WE have a document management system where by Adminstrators can upload documents, once the document is uploaded the binary data is stored on in a folder on the web server. We used to stored the documents in the actaul db table, but we found that there were to many documents and it was using alot of space on db server.

So my boss has decided we are now going to upload the binary data onto the web server. Currently we are donig this with new documents which have been added or documents which are gettinguploaded when reloading, but there are many documents in the db table which have not been updated and are still embedded in the db table. So i need to figure out how to go about copying the data storewd in the db table and storing it in web servers folder location.

I've tried various things for a enitre day but im going round in circiles.

MemoryStream mStream = new MemoryStream((Byte[])dtrResults["file"]);
BinaryReader bReader = new BinaryReader(mStream);
int intFileSize = (int)mStream.Length;
Byte[] byteFile = (Byte[])dtrResults["file"];

i can get to this state but then how do i create a folder on the BinaryREader to then store the binary data of the file to the location.

BinaryReader bReader2 = new BinaryReader(File.Open(strDocFolder + strSavedFileName, FileMode.Create));
int count2 = bReader2.Read(byteFile, 0, intFileSize);

bReader2.Close();

i've also tried this but when the file gets created in the folder there is no content.

i do know that the file does contain content as ive tried this and downlaoding the file from that page acctually works

string strContentTpe = WValue.WStr(dtrResults["contenttype"]);
int intFileSize = VValue.VInt(dtrResults["filesize"]);
/ Byte[] byteFile = ((Byte[])dtrResults["file"]);
//Downloads the data correctly
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + WValue.WStr(dtrResults["docfilename"]) + "\"");
Response.AddHeader("Content-Length", WValue.WStr(intFileSize));
Response.ContentType = strContentTpe;
Response.BinaryWrite(byteFile);

I hope ive made some snese andthat someone can hlep me.

Have a nice day

Zal


i dont think u should extract all the data by a response object .....how many files will u sit & click save .......bt just retrive the byte information from the database & send it across the network using socket programming.(dont get scared..it could be a command line utility)

while using socket programing u will need to make client which would extract the data & send it ...while at the new location could use a server which would recv data & and make files out of it.

this would only be advisable if the files are more than 50000.....

if less just write a command line utility to remove the data & store as .dat files with filenames......then physically transfer the data to new location

|||

Thanks for the advice, but it seems all a bit ot scary for me.

Ive actaully found a way of doing it by reading and writing to the reader, it might not be perfect but this code will only be used once.

Zal

No comments:

Post a Comment