The function below needs only the url path of a file in Sharepoint server, saves it at local directory and finally returns the file's path. It does not use the sharepoint client csom api. It also works with versions of a document, if you have enabled the versioning property of sharepoint document's library.
public string DownloadFile(string urlPath)
{
string serverTempdocPath = "";
try
{
var request = (HttpWebRequest)WebRequest.Create(urlPath);
var credentials = new NetworkCredential(configuration.Username, configuration.Password, configuration.Domain);
string docExtension = urlPath.Split('.')[1];
request.Credentials = credentials;
request.Timeout = 20000;
request.AllowWriteStreamBuffering = false;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
serverTempdocPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Guid.NewGuid() + "." + docExtension);
//Write to local disk
using (FileStream fs = new FileStream(serverTempdocPath, FileMode.Create))
{
byte[] read = new byte[256];
int count = stream.Read(read, 0, read.Length);
while (count > 0)
{
fs.Write(read, 0, count);
count = stream.Read(read, 0, read.Length);
}
}
}
}
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
return "";
}
return serverTempdocPath;
}