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?

sandy
  • 464
  • 1
  • 6
  • 13

1 Answers1

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();
}
Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
  • 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]` – sandy Nov 13 '19 at 12:21
  • 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 – sandy Nov 13 '19 at 12:34
  • @Johnathan : would you please give some technical reasons why not use webclient :-) because i am using webclient, would like to know if any drawback. – SP 2022 Nov 13 '19 at 16:45
  • @HabiburRahaman this is as good an explanation as any: https://stackoverflow.com/a/27737601/8126362 – Johnathan Barclay Nov 13 '19 at 16:57