Reputation: 711
What is the best way to save a file to my SQL database using Silverlight and LINQ?
I have read through some articles, some of them here on StackOverflow and there is so much information that I am not sure what is the best.
I have something that works using:
// Read the file
var reader = new StreamReader(openFileDialog.File.OpenRead());
contents = reader.ReadToEnd();
reader.Close();
// Convert to byte[]
byte[] inputbuffer;
var encoding = new UTF8Encoding();
inputBuffer = encoding.GetBytes(contents);
but according to something I read here on StackOverflow, using UTF8Encoding is not a good idea.
Also I can get the file from the database using LINQ when I need it, but how do I convert it back from the byte[] to the actual file?
Or would using WCF to save and retrieve a file be better?
Any ideas are greatly appreciated.
Upvotes: 0
Views: 746
Reputation: 52270
yes UTF8Encoding is not a good option. You can use the FileStream's copyto method to copy the files bytes into a memorystream and use it's ToArray method to get all bytes instead.
If you can access the DB directly from Silverlight than this should be ok but the second part of your questions indicates that you might not be sure(?) - if so please put this into another question.
Here is a snippet to return the bytes from the file:
var stream = openFileDialog.File.OpenRead();
using (var memStream = new System.IO.MemoryStream())
{
stream.CopyTo(memStream);
return memStream.ToArray();
}
To save it back you will have to use the SaveFileDialog class in silverlight
Upvotes: 1