public static void DownloadResource(String savePath, DownloadProgressChangedEventHandler onProgressChange, AsyncCompletedEventHandler onDownloadComplete, ErrorHandler errorHandler) { try { String fileNameParam = HashUtils.Base64Encode(savePath.Replace(TempUtil.ResourcePath, "")); String address = ChatConnection.Instance.WebHost; int port = ChatConnection.Instance.WebPort; Uri uri = new Uri(String.Format(ResourceDownloadUrl, address, port, currentVersion, fileNameParam)); WebClient webClient = RestUtils.CreateWebClient(); Directory.CreateDirectory(savePath.Replace(Path.GetFileName(savePath), "")); //Remove file name, create save path Directory.CreateDirectory(TempUtil.DownloadPath); //Create temp folder String temp = Path.Combine(TempUtil.DownloadPath, Guid.NewGuid().ToString()); //Create new temp file webClient.DownloadFileAsync(uri, temp); webClient.DownloadFileCompleted += (o, e) => { File.Move(temp, savePath); if (onDownloadComplete != null) { onDownloadComplete(o, e); } }; //webClient.DownloadFileAsync(uri, savePath); if (onProgressChange != null) { webClient.DownloadProgressChanged += onProgressChange; } webClient.DownloadFileCompleted += (o, e) => { RestUtils.Remove(webClient); }; } catch (Exception e) { Console.WriteLine(e); if (errorHandler != null) { errorHandler(e); } } }
public static void DownloadAttachment(String conversationID, String fileID, String savePath, DownloadProgressChangedEventHandler onProgressChange, AsyncCompletedEventHandler onDownloadComplete, ErrorHandler errorHandler, [CallerMemberName] string caller = null) { try { Console.WriteLine(caller + " downloading attachment..."); String address = ChatConnection.Instance.WebHost; int port = ChatConnection.Instance.WebPort; Uri uri = new Uri(String.Format(AttachmentDownloadUrl, address, port, fileID, conversationID)); WebClient webClient = RestUtils.CreateWebClient(); webClient.Headers.Add(ClientSession.HeaderToken, ChatConnection.Instance.Session.SessionID); Directory.CreateDirectory(TempUtil.DownloadPath); String temp = Path.Combine(TempUtil.DownloadPath, Guid.NewGuid().ToString()); webClient.DownloadFileAsync(uri, temp); webClient.DownloadFileCompleted += (o, e) => { File.Move(temp, RepairSavePath(savePath)); if (onDownloadComplete != null) { onDownloadComplete(o, e); } }; if (onProgressChange != null) { webClient.DownloadProgressChanged += onProgressChange; } webClient.DownloadFileCompleted += (o, e) => { RestUtils.Remove(webClient); }; } catch (Exception e) { Console.WriteLine(e); if (errorHandler != null) { errorHandler(e); } } }
public static void DownloadMedia(String streamURL, String savePath, DownloadProgressChangedEventHandler onProgressChange, AsyncCompletedEventHandler onDownloadComplete, ErrorHandler errorHandler, [CallerMemberName] string caller = null) { try { Console.WriteLine(caller + " downloading media..."); Uri uri = new Uri(streamURL); WebClient webClient = RestUtils.CreateWebClient(); Directory.CreateDirectory(TempUtil.DownloadPath); String temp = Path.Combine(TempUtil.DownloadPath, Guid.NewGuid().ToString()); webClient.DownloadFileAsync(uri, temp); webClient.DownloadFileCompleted += (o, e) => { File.Move(temp, RepairSavePath(savePath)); if (onDownloadComplete != null) { onDownloadComplete(o, e); } }; if (onProgressChange != null) { webClient.DownloadProgressChanged += onProgressChange; } webClient.DownloadFileCompleted += (o, e) => { RestUtils.Remove(webClient); }; } catch (Exception e) { Console.WriteLine(e); if (errorHandler != null) { errorHandler(e); } } }