/// <summary> /// Update both metadata and content of a file and return the updated file. /// </summary> public static Google.Apis.Drive.v2.Data.File UpdateResource(Google.Apis.Drive.v2.DriveService service, IAuthenticator auth, String fileId, String newTitle, String newDescription, String newMimeType, String content, bool newRevision) { // First retrieve the file from the API. Google.Apis.Drive.v2.Data.File body = service.Files.Get(fileId).Fetch(); body.Title = newTitle; body.Description = newDescription; body.MimeType = newMimeType; //byte[] byteArray = Encoding.ASCII.GetBytes(content); byte[] byteArray = Encoding.UTF8.GetBytes(content); MemoryStream stream = new MemoryStream(byteArray); Google.Apis.Drive.v2.FilesResource.UpdateMediaUpload request = service.Files.Update(body, fileId, stream, newMimeType); request.Upload(); Permission newPermission = new Permission(); newPermission.Type = "anyone"; newPermission.Role = "reader"; newPermission.Value = ""; newPermission.WithLink = true; service.Permissions.Insert(newPermission, fileId).Fetch(); return(request.ResponseBody); }
public string f_updateFile(DriveFile df_new) { if (this._authenticator == null || this._driveService == null) { return(JsonConvert.SerializeObject(new { Ok = false, tokenAccess = this._token, File = df_new, Message = "redirect user to authentication" })); } try { // First retrieve the file from the API. Google.Apis.Drive.v2.Data.File body = this._driveService.Files.Get(df_new.id).Fetch(); body.Title = df_new.title; body.Description = df_new.description; body.MimeType = df_new.mimeType; //byte[] byteArray = Encoding.ASCII.GetBytes(content); byte[] byteArray = Encoding.UTF8.GetBytes(df_new.content); MemoryStream stream = new MemoryStream(byteArray); Google.Apis.Drive.v2.FilesResource.UpdateMediaUpload request = this._driveService.Files.Update(body, df_new.id, stream, df_new.mimeType); request.Upload(); Permission newPermission = new Permission(); newPermission.Type = "anyone"; newPermission.Role = "reader"; newPermission.Value = ""; newPermission.WithLink = true; this._driveService.Permissions.Insert(newPermission, df_new.id).Fetch(); Google.Apis.Drive.v2.Data.File file = request.ResponseBody; return(JsonConvert.SerializeObject(new { Ok = false, tokenAccess = this._token, File = new DriveFile(file, df_new.content) })); } catch (Exception e) { return(JsonConvert.SerializeObject(new { Ok = false, tokenAccess = this._token, Message = e.Message, File = df_new })); } }