public void Upload(string webDavLocation, string localFile, WebDavOperationCallback operationProgress = null) { webDavLocation = webDavLocation.Replace("\\", "/").Trim('/'); var webDavFile = new Uri($"{this.BaseAddress}{webDavLocation}"); using (var stream = new FileStream(localFile, FileMode.Open)) { var putFileParams = new PutFileParameters(); putFileParams.OperationProgress = operationProgress; var response = this.PutFile(webDavFile, stream, putFileParams); if (!response.IsSuccessful) { throw new ApplicationException($"Unable save file: --> {response.StatusCode} {response.Description}"); } } var name = XName.Get("x-lastmodified", "DataSync"); FileInfo fi = new FileInfo(localFile); int attempt = 5; while (true) { var patch = new ProppatchParameters(); patch.PropertiesToSet.Add(name, fi.LastWriteTimeUtc.ToString("u")); patch.Namespaces.Add(new NamespaceAttr("u", "DataSync")); var propPatch = this.Proppatch(webDavFile, patch); if (propPatch.IsSuccessful) { break; } attempt = attempt - 1; if (attempt == 0) { throw new ApplicationException($"Unable update file properties: --> {propPatch.StatusCode} {propPatch.Description}"); } Thread.Sleep(1000); } }
public void Download(string webDavLocation, string localFile, WebDavOperationCallback operationProgress = null) { webDavLocation = webDavLocation.Replace("\\", "/").Trim('/'); var webDavFile = new Uri($"{this.BaseAddress}{webDavLocation}"); operationProgress?.Invoke(new WebDavOperationInfo { Progress = 0 }); using (var outputStream = new FileStream(localFile, FileMode.Create)) using (var response = this.GetFile(webDavFile, false, new GetFileParameters())) { if (!response.IsSuccessful) { throw new ApplicationException($"Unable read file: --> {response.StatusCode} {response.Description}"); } long count = response.Stream.Length; byte[] buf = new byte[32768]; while (count > 0) { int nRead = (count < buf.Length) ? (int)count : buf.Length; nRead = response.Stream.Read(buf, 0, nRead); outputStream.Write(buf, 0, nRead); count -= nRead; operationProgress?.Invoke(new WebDavOperationInfo { Progress = 100.0 * (response.Stream.Length - count) / response.Stream.Length }); } operationProgress?.Invoke(new WebDavOperationInfo { Progress = 100.0 }); } }