/// <summary> /// This method creates and uploads a file into a parent folder /// </summary> /// <param name="driveId">The ID of the target drive</param> /// <param name="parentFolderId">The ID of the parent folder</param> /// <param name="file">The file object</param> /// <param name="content">The binary stream of the file content</param> /// <param name="contentType">The content type of the file</param> /// <returns>The just created and uploaded file object</returns> public static DriveItem UploadFile(String driveId, String parentFolderId, DriveItem file, Stream content, String contentType) { var jsonResponse = MicrosoftGraphHelper.MakePostRequestForString( String.Format("{0}drives/{1}/items/{2}/children", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveId, parentFolderId), file, "application/json"); var uploadedFile = JsonConvert.DeserializeObject <DriveItem>(jsonResponse); try { MicrosoftGraphHelper.MakePutRequest( String.Format("{0}drives/{1}/items/{2}/content", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveId, uploadedFile.Id), content, contentType); } catch (ApplicationException ex) { // For whatever reason we come here ... the upload failed // and we need to delete the just created file FilesHelper.DeleteFile(driveId, uploadedFile.Id); // And then we re-throw the exception throw ex; } return(uploadedFile); }
/// <summary> /// Uploads a new file content on top of an already existing file /// </summary> /// <param name="driveId">The ID of the target drive</param> /// <param name="fileId">The ID of the target file</param> /// <param name="content">The binary stream of the file content</param> /// <param name="contentType">The content type of the file</param> public static void UpdateFileContent(String driveId, String fileId, Stream content, String contentType) { MicrosoftGraphHelper.MakePutRequest( String.Format("{0}drives/{1}/items/{2}/content", MicrosoftGraphHelper.MicrosoftGraphV1BaseUri, driveId, fileId), content, contentType); }