question

AmitRawat-3636 avatar image
0 Votes"
AmitRawat-3636 asked MichaelHan-MSFT commented

CSOM OpenBinaryDirect read chunk wise c#.

Hi, I am using OpenBinaryDirect to download file from SharePoint online.
When I am using fileInfo.Stream.CopyTo(filestream), code is able to download file.

var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);

//Below is the working code.
using (FileStream filestream = new FileStream("C:" + "\\" + file.Name, FileMode.Create))
{
fileInfo.Stream.CopyTo(filestream);
}

But when I am using below code, for some files it is not downloading complete file. The file which is giving me error is of size 4Mb but below code only download 1.5MB and because of that I am getting file corrupted error.

//Code which is not downloading complete file
byte[] buffer = new byte[1024 * 100];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}

office-sharepoint-online
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered MichaelHan-MSFT converted comment to answer

Hi @AmitRawat-3636

Per my test, it works on my end. What kind of these files? Are they special? Please share more details. Below is my demo code:

         using(FileStream filestream = new FileStream("C:" + "\\" + file.Name, FileMode.Create))
         {
             byte[] buffer = new byte[1024];
             int len;
             while ((len = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0)
             {
                 filestream.Write(buffer, 0, len);
             }
         }



If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AmitRawat-3636 avatar image
0 Votes"
AmitRawat-3636 answered MichaelHan-MSFT commented

Working for me as well, except for one doc file.
When I download this file via browser and upload it again, that too is behaving as expected.
Not sure what's the problem.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AmitRawat-3636

Since that it worked, you could accept your answer.

0 Votes 0 ·