Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am uploading a document to sharepoint site using client object model.

using (FileStream fs = new FileStream(@"C:\temp\UlDoc.docx", FileMode.Open))
{
    File.SaveBinaryDirect(_cc, "/demo/Word%20Docs/temp.docx", stream, true);
}

The file is uploading to the site with same filename but it is getting corrupted (only 18kb) instead of the actual file size. Any help!

Thanks.

share|improve this question
add comment

2 Answers 2

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);
share|improve this answer
    
Thank you for the answer, I can see it will work as I had similar code already implemented to upload a file given a "local filepath" of the document. However I am trying to upload when a stream is passed (part of the requirement as I get a stream from Document automation app), and using "SaveBinaryDirect" as the above approach fails with large files (word documents). –  Flowerking Apr 24 '12 at 18:09
1  
Increasing the Maximum Message Size msdn.microsoft.com/en-us/library/ff599489.aspx –  Basant B. Pandey Apr 25 '12 at 3:49
    
+1 For your effort, But I still prefer using SaveBinaryDirect as That is the problem here. Not any workarounds. Thank you. –  Flowerking Apr 25 '12 at 8:39
add comment

I believe that only the default buffer is loaded at maximum and that is why is corrupted. Try looping through the array as such :

FileStream fstream = File.OpenRead(<file_location_here>);
            byte[] content = new byte[fstream.Length];
            fstream.Read(content, 0, (int)fstream.Length);
            fstream.Close();

Now the FileStream should have it all loaded.

use then an SPWeb previously opened and the method Add as in

using (SPSite _site= new SPSite("URL"))
{
 using (SPWeb _web = _site.OpenWeb()){
_web.Files.Add("<FULL LIB PATH>/<NAME OF THE FILE>", content);
}
}
share|improve this answer
    
Thank you for the reply, I have already tried reading the file stream the way you suggested, but it is saving a blank file instead of the actual file. And I cannot use SPWeb as I am using client object model and the upload is from client side. –  Flowerking Apr 24 '12 at 14:55
    
Oh, i didn't catch on the C-OM. There are valid example on how to upload it via OM (assuming Managed OM - right?). See here blogs.msdn.com/b/sridhara/archive/2010/03/12/… and here blog.isrichtig.de/Lists/Beitraege/… –  C. Marius - MVP Apr 24 '12 at 14:59
    
Nice links, thank you. I am exactly doing it as in the examples in your links, (you can see my code in the question). The only problem is corrupted document. –  Flowerking Apr 24 '12 at 15:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.