Click here to Skip to main content
15,908,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Requirement is like extract the data from the oracle database which will be in BLOB datatype. I need to download the blob data based upon the content type like say if it is .csv or .xlsx or .pdf in the readable way.


Please help me out from this.

What I have tried:

//while (reader.Read())
//{
// //byte[] stream = (byte[])reader["DATA"];
// string fileName = (string)reader["FILE_NAME"];
// string contentType = (string)reader["FILE_TYPE"];
// //WriteFile(stream, fileName, contentType, HttpContext.Current.Response);


// FileStream fs; // Writes the BLOB to a file (*.bmp).
// BinaryWriter bw; // Streams the BLOB to the FileStream object.
// int bufferSize = 100; // Size of the BLOB buffer.
// byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes.
// long retval; // The bytes returned from GetBytes.
// long startIndex = 0; // The starting position in the BLOB output.



// // Create a file to hold the output.
// fs = new FileStream(@"C:\Users\V.Manjunatha\Desktop\Packages\" + fileName,
// FileMode.OpenOrCreate, FileAccess.Write);
// bw = new BinaryWriter(fs);

// // Reset the starting byte for the new BLOB.
// startIndex = 0;

// // Read the bytes into outbyte[] and retain the number of bytes returned.
// retval = reader.GetBytes(1, startIndex, outbyte, 0, bufferSize);

// // Continue reading and writing while there are bytes beyond the size of the buffer.
// while (retval == bufferSize)
// {
// bw.Write(outbyte);
// bw.Flush();

// // Reposition the start index to the end of the last buffer and fill the buffer.
// startIndex += bufferSize;
// retval = reader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
// }

// // Write the remaining buffer.
// bw.Write(outbyte, 0, (int)retval);
// bw.Flush();

// // Close the output file.
// bw.Close();
// fs.Close();

//}

When i use this code the .csv file as


‹„½K®¹ŽžÛ7à™l$ô~´OàÎà€«cÜƝ?
"I_ÿãýÿÿÿþûþçßâßÒßòßý¿åïÿü§üý×êúgýïÿ-ç8þ ñ/ü“cš9·ôðŸHÿ þ”OHKr¤ÿF7äþ©¨2)Pgû"

´6ÎËŸ'Q §ú—ÇŒÀ
Œ6ÿJñ£@&iXPž³BòŠ9¬XRÿ埐(KC‚ÿÒ `âÈ6€R[ׯßXY4[µÆ
¤I(¶†ëÇÒÈÓ:I{rþ¼‹eŒ¿ÄOXÄòŽú?@¬ý/•õÎ#b`Ȩëéçùòùºžðˆ¥çú"Å.1õ×CüÊqÿÒT
=ÌnkÒ(í4Õ˜@gzZïc}Ì@*ôRúS·œU¨Í†¨ÐË°)* ú
#F(¿¥±f½‘jýÈ#k0òšé€44™rú£F篓ܾ®/Îd(Ž:Òßhýûx(Ž¦-/–êHåþm:€ŒÃÑ“sýÊ#yÐãóz. ‘tý6G*
Ì5››€ŒÂ1s÷V`®q͸}2L×Ð!éz[o©*Š
LžÍ—†Ó¾þÔ'×+J±l@¦³ºþÿfyéã[ÿ*#pÆÐl ²3ŽéP`¦µ˜þ<ó0¥¢@¡'ã¼€>dQÀÜ~­u¶x­ñ-"继õfÊŸPeÎØ›
4H×Ï·¥|ڏ9r¾ÿZg¬á#ïzû\l@v„KOÎ
4R¿|ßÏP¾?Á©8@ —¶\Aòä¬AX‹éúí­b‹<‘Ï€g`I¿âµÚ;@寰{ržÈM-ՏŽ™UX_¡-/¢A[ÃÁx"¥‰¾]Ÿ‰|æMÆáŠ
TK봁¦*äìP¡Î%¯µ|B絤:Ò½–Øò¡KIw.0®¥¤Ô{ã'$??:RýùÝ”÷ +É’­åìó¡uÙŽ¥›
"°t l©Nä4"
Ùs]O·ï1Xó
„ •„~¢d–Ò<}ýÀ-•y4„db˜¬…¤x€|ÈÁø3]ŸÀ IŸ@:R]IÖ4¦+IÍ sY´ÎÓÀp¤úh»3ec¿#Ù®wäCÇÀ²<m@&ähy¬µá3õvýöfcç`·å2ƒ$gª3í¥êg„ž¨3]þtöè\awƒ>}akO— ¹<€˜m ¨
)ÙÀÞšåæQ!m€UÀ†Õ”‹
ôX@Ó™3±ôÇžT€üøEÐß·nˆå=à1€Ø%˜ˆ
"óÏˆ`âú‡ À"
ˆY²v
ô9V[Ž}á’¯ÕÝ`˜
}àtv5öü°—9v¤ÐAs”
"`†«¹zržà×–æoæþùzØ‘BÀú&0u’kÓ 1ÏÔ£!gÈìß@=€:RfË°‰Æ‹eVÚÛ–¹‚ó5CŽU4‘Ÿ%Çïýõ;Çbb²¬ë?®. îF¹äxÏð¤L82ôøéþk1YÈc}ÉÏíe(2wÆ Õx™¡½Œ 4¼a:\4¥²ÌÐ"
9irx:
;-ãO À
Œî_Êßœù{û!3"ãð¤2¡ñ¯4{B» }–:'½‰!Só"
´3ÝYRÙ
>ŸJՁXzµÝ2æ^ž@Þ€ÌfkÃ&´cVl


which is not readble.
Posted
Updated 22-Jun-16 0:05am
Comments
BillWoodruff 22-Jun-16 6:01am    
I am confused by your question. If you know what the data format is (.pdf, etc.), shouldn't you create the blob by serializing the file to binary form, and then save that with some linked identifying tag so you know what the blob "is" ?

Then when you need to re-construct the file, you get the blob type and then get the blob data and pass it to the appropriate de-serializer.
Member 12597547 22-Jun-16 6:42am    
Yes i have blob type how to de-serialize it to appropriate type. Can you please suggest me out
BillWoodruff 22-Jun-16 8:03am    
There are lots of tools for getting .csv or .xslx content into some representation, and lot of tools for constructing .pdf files and viewing them in .NET. Time for you to do some research.

You could, for example, inter-operate with Excel using automation.

1 solution

How I do it:
First you get the bytes from database:
C#
private byte[] GetFile(string id)
{
	using (SqlConnection con = new SqlConnection("CONNECTION STRING")
	{
		using (SqlCommand cmd = con.CreateCommand())
		{
			cmd.CommandText = "Select COLUMN from TABLE where id = @id";
			cmd.Parameters.AddWithValue("@id", id);
			con.Open();
			return (byte[])cmd.ExecuteScalar();
		}
	}
}


Then save file:

C#
var byteFile = GetFile(name,tableName, urn);
if (table.Extension == "pdf")
{
	File.WriteAllBytes("PATH/NAME", byteFile);
}
else
{
	string text = Encoding.Unicode.GetString(byteFile);
	File.WriteAllText("PATH/NAME", text);
}


Sometimes you need write bytes (ie: pdf), sometime it is better to write text. For text don't forget to specify correct encoding.
 
Share this answer
 
v2
Comments
Member 12597547 22-Jun-16 6:41am    
When i use the above code i am the output as follow which is wrong

²´ç¢¨ï‚Ÿî™†ì³¸ä‹‹Ã§éŸµí„“봥旦酝₰鞞䎵꿒콱ֽ㽱郟㏙௪⡲ጲ惄寮崐鉅ᖗᵭ㉟⻕版墲ꢩꮛ╜䍎쩦ۜ搔麇栿뚔ᲂ㈋ⅾ벗䤜젟賘䓛숵釰ﲰ㺰ᝆ帤㒎쯏⑲⁼⼜荿쵗霑ꌯ ̄Ꜽ䭮㳈逞咹燇śퟺ扸빖⸣墲홢葹劏㵼襟䫹ꐖ젯녥ꄢ�꬗曧∽㖖냗⑞蜺鐺퀞뫝⋬罍㘴䢁⩫曖澷쮳赔챖ჳ랗鴦ὶ妁턻㓧萶�嚕褫ᑹ뫉ۀ頥Ⴁ狾脬蟅2貗嚺慕챒懹㼠ᨅ쌡廲⿎묅謂ਯ뾡븡洩뽗쨣怳

Can you suggest me like how to deseriliaze the byte[]
xszaboj 22-Jun-16 8:23am    
Hi, yes I can help you, just need more info. What file are you trying to deserialize? Did you tried different encoding? Can you show us how you serialize file into byte[]?
Member 12597547 22-Jun-16 8:30am    
i tried with this lines which you have provided for encoding with right path
string text = Encoding.Unicode.GetString(datainBytes);
File.WriteAllText(@"C:\Users\V.Manjunatha\Desktop\Packages\" + fileName, text);

And the content type is .csv for encoding with above lines, it is showing the result in unreadable format as provided before.

I dont have any file to deserialize. It is just a Blob data(data field) which is coming from oracle DB.
xszaboj 22-Jun-16 9:20am    
Maybe try the writeallbytes path. Can you also be sure it is cvs? Or different encoding then unicode

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900