public async Task SaveAsync(FailedEvent @event) { if (@event == null) { return; } await Wrapper.SaveAsync(CollectinoName, @event, x => x.Id == @event.Id); }
/// <summary> /// 下载文件 /// </summary> /// <param name="fileId"></param> /// <param name="savePath">文件保存路径</param> public void DownLoadFile(DriveService driveService, string fileId, long fileSize, string savePath) { if (driveService == null) { return; } var request = driveService.Files.Get(fileId); var stream = new FileStream(savePath, FileMode.Create); // Add a handler which will be notified on progress changes. // It will notify on each chunk download and when the // download is completed or failed. try { request.MediaDownloader.ChunkSize = 8192;//配置chunk大小 request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) => { switch (progress.Status) { case DownloadStatus.Downloading: { Console.WriteLine(progress.BytesDownloaded); ProgressEvent?.Invoke(progress.BytesDownloaded, fileSize); break; } case DownloadStatus.Completed: { Console.WriteLine("Download complete."); FinishedEvent?.Invoke(); break; } case DownloadStatus.Failed: { Console.WriteLine("Download failed."); FailedEvent?.Invoke(); break; } } }; request.Download(stream); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } finally { if (stream != null) { stream.Close(); stream = null; GC.Collect(); } } }
public AuthController(SuccessEvent success, FailedEvent failed) { onSuccess += success; onFailed += failed; user = Library.collection.db.users.FirstOrDefault(); if (user != null) { SocketController.instance.Auth(user.email, user.password); onSuccess(); } else { SocketController.instance.callbacks.onAuth += OnAuth; } }
public async Task <bool> UpLoadFile(GraphServiceClient graphClient, string sourcePath, string fileID = null) { try { if (!System.IO.File.Exists(sourcePath)) { return(false); } System.IO.FileInfo info = new System.IO.FileInfo(sourcePath); long fileSize = info.Length; string fileName = System.IO.Path.GetFileName(sourcePath); long currentSize = 0; using (FileStream fileStream = new FileStream(sourcePath, FileMode.Open)) { // Create the upload session. The access token is no longer required as you have session established for the upload. // POST /v1.0/drive/root:/UploadLargeFile.bmp:/microsoft.graph.createUploadSession UploadSession uploadSession; if (string.IsNullOrEmpty(fileID)) { uploadSession = await graphClient.Me.Drive.Root.ItemWithPath(fileName).CreateUploadSession().Request().PostAsync(); } else { uploadSession = await graphClient.Me.Drive.Items[fileID].ItemWithPath(fileName).CreateUploadSession().Request().PostAsync(); } var maxChunkSize = 320 * 1024; // 320 KB - Change this to your chunk size. 5MB is the default. var provider = new ChunkedUploadProvider(uploadSession, graphClient, fileStream, maxChunkSize); // Setup the chunk request necessities var chunkRequests = provider.GetUploadChunkRequests(); var readBuffer = new byte[maxChunkSize]; var trackedExceptions = new List <Exception>(); DriveItem itemResult = null; //upload the chunks foreach (var request in chunkRequests) { // Do your updates here: update progress bar, etc. // ... // Send chunk request var result = await provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions); //这里时最终的一个结果 if (result.UploadSucceeded) { itemResult = result.ItemResponse; currentSize = (long)result.ItemResponse.Size; } else { currentSize += maxChunkSize; } ProgressEvent?.Invoke(currentSize, fileSize); } // Check that upload succeeded if (itemResult == null) { // Retry the upload // ... FailedEvent?.Invoke(); } else { FinishedEvent?.Invoke(); } return(true); } } catch (ServiceException e) { System.Diagnostics.Debug.WriteLine("We could not upload the file: " + e.Error.Message); FailedEvent?.Invoke(); return(false); } }
/// <summary> ///上传文件到Cloud,如果传递同名的文件有什么影响 /// </summary> /// <param name="sourcePath"></param> public void UpLoadFile(DriveService driveService, string sourcePath, string parentId = null) { if (driveService == null) { return; } if (!System.IO.File.Exists(sourcePath)) { return; } System.IO.FileInfo info = new System.IO.FileInfo(sourcePath); long fileSize = info.Length; string fileName = System.IO.Path.GetFileName(sourcePath); var contentType = MimeMapping.GetMimeMapping(fileName); var fileMetadata = new File() { Name = fileName }; if (!string.IsNullOrEmpty(parentId)) { fileMetadata.Parents = new List <string>() { parentId }; } FilesResource.CreateMediaUpload request; using (var stream = new System.IO.FileStream(sourcePath, System.IO.FileMode.Open)) { request = driveService.Files.Create( fileMetadata, stream, contentType); request.Fields = "id"; request.ChunkSize = 262144;//配置chunk大小,must be a multiple of Google.Apis.Upload.ResumableUpload.MinimumChunkSize request.ProgressChanged += (IUploadProgress progress) => { switch (progress.Status) { case UploadStatus.Uploading: { // Console.WriteLine(progress.BytesSent); ProgressEvent?.Invoke(progress.BytesSent, fileSize); break; } case UploadStatus.Completed: { Console.WriteLine("Upload complete."); FinishedEvent?.Invoke(); break; } case UploadStatus.Failed: { Console.WriteLine("Upload failed."); FailedEvent?.Invoke(); break; } } }; request.Upload(); } var file = request.ResponseBody; Console.WriteLine("File ID: " + file.Id); }