Wrong data in file downloaded via webclient.DownloadFile

Lou Yovin 96 Reputation points
2021-11-09T15:00:29.807+00:00

Console program written in C# has been running for several years as a cron job. I down load documents from a sharepoint site containing document libraries for several (~20) users. Users sync to their library on their PC's. Each library contains documents (excel and pdf) in the root as well as in sub-folders. The program connects to the site, using sharepoint credentials and then downloads all new/changed documents to our on-site server using the webclient.download method. This worked well until a couple of months ago. Now, documents in the root contain incorrect data. Documents in the sub folders have correct data. Downloading via the GUI and via sync is correct. Example document named test.txt, containing "this is text", contains this (much truncated to be able to post):


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"


Downloading via REST API resolves the problem for the root, I am still using the downloadfile method for sub folders

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,374 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,248 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lou Yovin 96 Reputation points
    2021-11-09T19:43:12.197+00:00

    The method return is void. It just downloads the file into the target folder.
    I think we are on different pages here. Anyway, thank you very much for the help. Since I have a work around, I am OK. Mostly posted in case someone else has the same problem.

    Lou


2 additional answers

Sort by: Most helpful
  1. Michael Taylor 48,281 Reputation points
    2021-11-09T15:57:26.417+00:00

    Just a rough guess at this point since you didn't post any code but I'm assuming there is an error in that HTML. Can you post the request and response code you're using, the HTTP status code and the contents of the HTML?

    DownloadFile doesn't do anything special. It literally just sends a GET request to the given URL and anything that is returned back is dumped to a file. Whether the content was a file to begin with or not isn't relevant to this method.

    0 comments No comments

  2. Lou Yovin 96 Reputation points
    2021-11-09T17:52:06.263+00:00

    Program is a Console program written in C#
    much code to get the list of users from SP and then get file last modified dates, etc.
    Down load Code goes something like this, simplified:
    get sharepoint credentials
    var client = new WebClient();
    client.getCredentials (a method written to get credentials)
    client.DownloadFile(webUrl (to doc library containing file), Targetfolder+@"\"+filename);

    As I said, program has worked OK for several years.