1

my company is working with an Excel-file (xlsx) on Sharepoint. For my application I need the content of the file as a byte array.

When located on my local machine, I would use System.IO.File.ReadAllBytes(path).

How can I do the same with a file hosted on a server? The url is something like "https://sharepoint.com/excel.xlsx"

I tried new WebClient().DownloadData(url) but this returns something different I can't use. I think it returns the byte array of the file itself and not the content of the file.

Any ideas?

1 Answer 1

4

Rather than WebClient, try HttpClient:

using (var client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync("https://sharepoint.com/excel.xlsx"))
{
    byte[] fileContents = await response.Content.ReadAsByteArrayAsync();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately response.Content seems not to be the content of the excelfile. With ReadAllBytes(localPath) I get byte[18491], with the httpResponse only byte[16]
Sorry, forget it. My response get always a 401 error. have to look how i can solve that before i can say if your solution was helpful for me
@Johnathan : would you please give some technical reasons why not use webclient :-) because i am using webclient, would like to know if any drawback.
@HabiburRahaman this is as good an explanation as any: stackoverflow.com/a/27737601/8126362

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.