public static string uploadJson(DriveService service, string file_path, Google.Apis.Drive.v2.Data.File _folder, string file_id = null) { try { if (!CheckInternetConnection()) { return(null); } var uploadStream = new FileStream(file_path, FileMode.Open, FileAccess.Read); // Create or update the JsonTable if (file_id == null) { // Create it InsertMediaUpload uploadRequest = service.Files.Insert( new Google.Apis.Drive.v2.Data.File { Title = Path.GetFileName(file_path), Parents = new List <ParentReference>() { new ParentReference() { Id = _folder.Id } } }, uploadStream, MIME_JSON); var task = uploadRequest.Upload(); uploadStream.Dispose(); return(uploadRequest.ResponseBody.Id); } else { // Update it UpdateMediaUpload uploadRequest = service.Files.Update( new Google.Apis.Drive.v2.Data.File { Title = Path.GetFileName(file_path), Parents = new List <ParentReference>() { new ParentReference() { Id = _folder.Id } } }, file_id, uploadStream, MIME_JSON); var task = uploadRequest.Upload(); uploadStream.Dispose(); return(uploadRequest.FileId); } } catch (WebException e) { throw new WebException("There are issues with the connection. Maybe try again later. Full error message: " + e.Message); } }
// Upload functions public static async void UploadGame(DriveService service, string file_path, Google.Apis.Drive.v2.Data.File _folder, Action <IUploadProgress, BackgroundWorker, string, long> onFileUploadProgressChanged, Action <Google.Apis.Drive.v2.Data.File, BackgroundWorker> onFileUploaded, BackgroundWorker sendingworker, string file_id = null) { if (!CheckInternetConnection()) { sendingworker.ReportProgress(0, new StatusHolder() { txt = "Not connected to the internet. Aborted.", progress = -1 }); sendingworker.CancelAsync(); return; } try { // Reset the WaitHandler stopWaitHandle.Reset(); // Load the file var uploadStream = new FileStream(file_path, FileMode.Open, FileAccess.Read); // Check if the file should be overwritten or newly created if (file_id != null) { // Update existing file UpdateMediaUpload uploadRequest = service.Files.Update( new Google.Apis.Drive.v2.Data.File { Title = Path.GetFileName(file_path), Parents = new List <ParentReference>() { new ParentReference() { Id = _folder.Id } } }, file_id, uploadStream, MIME_7ZIP); uploadRequest.ChunkSize = ResumableUpload <InsertRequest> .MinimumChunkSize; uploadRequest.ProgressChanged += (progress) => onFileUploadProgressChanged(progress, sendingworker, Path.GetFileNameWithoutExtension(file_path), new FileInfo(file_path).Length); uploadRequest.ResponseReceived += (file) => onFileUploaded(file, sendingworker); var task = uploadRequest.UploadAsync(); await task.ContinueWith(t => { uploadStream.Dispose(); }); } else { // Create new file InsertMediaUpload uploadRequest = service.Files.Insert( new Google.Apis.Drive.v2.Data.File { Title = Path.GetFileName(file_path), Parents = new List <ParentReference>() { new ParentReference() { Id = _folder.Id } } }, uploadStream, MIME_7ZIP); uploadRequest.ChunkSize = ResumableUpload <InsertRequest> .MinimumChunkSize; uploadRequest.ProgressChanged += (progress) => onFileUploadProgressChanged(progress, sendingworker, Path.GetFileNameWithoutExtension(file_path), new FileInfo(file_path).Length); uploadRequest.ResponseReceived += (file) => onFileUploaded(file, sendingworker); var task = uploadRequest.UploadAsync(); await task.ContinueWith(t => { uploadStream.Dispose(); }); } // Wait for whichever comes first: User aborts the task; Upload was successful stopWaitHandle.WaitOne(); // Clear the local fileStream. Note that this will also abort any current upload tasks. uploadStream.Dispose(); } catch (WebException e) { sendingworker.ReportProgress(0, new StatusHolder() { txt = "There are issues with the connection. Aborted.", progress = -1 }); sendingworker.ReportProgress(0, new StatusHolder() { txt = "Error message: " + Environment.NewLine + e.Message, progress = -1 }); sendingworker.CancelAsync(); } }