public async Task UploadFile(string FileName, string GDriveID = null, string MIMEType = "application/octet-stream") { string format = AuxFileCommCenter.GetFileNameOnlyWithFormat(FileName)[1]; if (!string.IsNullOrEmpty(format)) { MIMEType = AuxFileCommCenter.GuessMime(format) ?? "application/octet-stream"; } await UploadFileAsync(this.Service, FileName, GDriveID, MIMEType); }
public Task <IUploadProgress> UploadFileAsync(DriveService service, string fileName, string GDFolderID = null, string MIMEType = "application/octet-stream") { Dictionary <string, string> fls = FindFiles(false); FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); string filenamewithoutpath = AuxFileCommCenter.GetFileNameOnlyWithFormat(fileName)[0]; Google.Apis.Drive.v3.Data.File FileMeta = null; bool Existe = fls.ContainsValue(filenamewithoutpath); Task <IUploadProgress> tk; if (Existe) { string fileid = fls.Where(x => x.Value == filenamewithoutpath).First().Key; var insert = service.Files.Update(new Google.Apis.Drive.v3.Data.File { Name = filenamewithoutpath ?? fileName }, fileid, fs, MIMEType); IList <string> oldparents = null; if (!string.IsNullOrEmpty(GDFolderID)) { var getolderfolder = service.Files.Get(fileid); getolderfolder.Fields = "parents"; var fileData = getolderfolder.Execute(); oldparents = fileData.Parents; if (oldparents != null) { if (!oldparents.Contains(GDFolderID)) { insert.RemoveParents = string.Join(",", oldparents); insert.AddParents = GDFolderID; } } else { insert.AddParents = GDFolderID; } } insert.ProgressChanged += Upload_Progress; insert.ResponseReceived += Upload_Anwser; BytesTotal = fs.Length; tk = insert.UploadAsync(); } else { if (!string.IsNullOrEmpty(GDFolderID)) { FileMeta = new Google.Apis.Drive.v3.Data.File() { Name = filenamewithoutpath ?? fileName, Parents = new List <string>() { GDFolderID } }; } else { FileMeta = new Google.Apis.Drive.v3.Data.File() { Name = filenamewithoutpath ?? fileName }; } var insert = service.Files.Create(FileMeta, fs, MIMEType); insert.ProgressChanged += Upload_Progress; insert.ResponseReceived += Upload_Anwser; BytesTotal = fs.Length; tk = insert.UploadAsync(); } tk.ContinueWith(t => { Console.WriteLine("Falha no upload do arquivo"); }, TaskContinuationOptions.NotOnRanToCompletion); tk.ContinueWith(t => { fs.Close(); fs.Dispose(); Console.WriteLine("Done! Upload Complete"); }); return(tk); }