This is helper method to return the bytes form the stream. Please let me know if you face any issue with this code.
I am using the same code in SilverLight Application.
public static byte[] ReadFully(Stream input, long fileSize)
{
//long fileSize = input.Length;
byte[] buffer = new byte[fileSize];
// byte[] newArray = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
Use var localFile = new FileCreationInformation() for saving a file using client Object model.
List docs = context.Web.Lists.GetByTitle("test");
var localFile = new FileCreationInformation();
localFile.Content = ReadFully(_openFileDialog.File.OpenRead(),
_openFileDialog.File.Length);
// localFile.Overwrite = true;
string FileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(_openFileDialog.File.Name);
string FileExtension = System.IO.Path.GetExtension(_openFileDialog.File.Name);
localFile.Overwrite = true;
localFile.Url = System.IO.Path.GetFileName(_openFileDialog.File.Name);
file = docs.RootFolder.Files.Add(localFile);
context.Load(file);
text= file.ListItemAllFields;
text["Title"] = file.Name;
text.Update();
context.Load(file.ListItemAllFields);
context.ExecuteQueryAsync(sucess, failed);