Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I would like to know what are benefits/drawbacks when using either of these methods for downloading a document from Sharepoint

using Microsoft sharepoint library

public void Download(string serverFilePath, string destPath) {
    CreateDirectoryIfNotExists(Path.GetDirectoryName(destPath));
    using (FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(_clientCtx, serverFilePath)) {
        using (Stream destFile = System.IO.File.OpenWrite(destPath)) {
            byte[] buffer = new byte[8 * 1024];
            int len;
            while ((len = ffl.Stream.Read(buffer, 0, buffer.Length)) > 0) {
                destFile.Write(buffer, 0, len);
            }
        }
    }
}

as oposed to downloading it using WebRequest

public void DownloadFile(string serverFilePath, string destPath) {
    var url = string.Format("{0}/{1}", ServerURL, serverFilePath);
    CreateDirectoryIfNotExists(Path.GetDirectoryName(destPath));
    var request = System.Net.HttpWebRequest.Create(url);
    request.Credentials = _clientCtx.Credentials;
    using (var sr = new StreamReader(request.GetResponse().GetResponseStream())) {
        using (var sw = new StreamWriter(destPath)) {
            sw.Write(sr.ReadToEnd());
        }
    }
}

because using the HttpRequest results in approximately 30% better performance.

Thanks in advance.

share|improve this question

1 Answer 1

I don't mean to nitpick but your first example is a really bad approach. You are reallocating your buffer every iteration and using a truly tiny buffer at that. Try this approach instead.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.