public async Task <bool> UploadFileAsync(GraphServiceClient graphService, UploadFileModel model) { try { var graphToken = AuthenticationHelper.GetGraphAccessTokenAsync(); var propertyGroup = await graphService.GetGroupByDisplayNameAsync(model.PropertyTitle); if (propertyGroup == null) { return(false); } DriveItem driveItem = new DriveItem(); driveItem.Name = model.File.FileName; driveItem.File = new Microsoft.Graph.File(); driveItem = await graphService.Groups[propertyGroup.Id].Drive.Root.Children.Request().AddAsync(driveItem); await graphService.Groups[propertyGroup.Id].Drive.Items[driveItem.Id].Content.Request().PutAsync <DriveItem>(model.File.InputStream); } catch (Exception el) { throw el; } return(false); }
public async Task <bool> UploadFileAsync(Graph.GraphService graphService, UploadFileModel model) { var graphToken = AuthenticationHelper.GetGraphAccessTokenAsync(); var propertyGroup = await graphService.GetGroupByDisplayNameAsync(model.PropertyTitle); if (propertyGroup == null) { return(false); } string id = string.Empty; string requestUri = string.Format("{0}{1}/groups/{2}/files", AADAppSettings.GraphResourceUrl, AppSettings.DemoSiteCollectionOwner.Split('@')[1], propertyGroup.objectId); //create placeholder HttpWebRequest reqCreatePlaceholder = (HttpWebRequest)HttpWebRequest.Create(requestUri); reqCreatePlaceholder.Method = "POST"; reqCreatePlaceholder.Headers.Add("Authorization", await graphToken); reqCreatePlaceholder.ContentType = "application/json"; string content = string.Format("{{type:'File',name:'{0}'}}", model.File.FileName); byte[] contentBody = System.Text.ASCIIEncoding.UTF8.GetBytes(content); using (Stream dataStream = reqCreatePlaceholder.GetRequestStream()) { dataStream.Write(contentBody, 0, contentBody.Length); } try { using (HttpWebResponse response = (HttpWebResponse)reqCreatePlaceholder.GetResponse()) { if (response.StatusCode == HttpStatusCode.Created) { string responseString = (new StreamReader(response.GetResponseStream())).ReadToEnd(); id = Newtonsoft.Json.Linq.JObject.Parse(responseString)["id"].ToString(); } } } catch { throw; } if (string.IsNullOrEmpty(id)) { return(false); } //upload file HttpWebRequest req_uploadFile = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}/{1}/uploadContent", requestUri, id)); req_uploadFile.Method = "POST"; req_uploadFile.Headers.Add("Authorization", await graphToken); using (var dataStream = req_uploadFile.GetRequestStream()) await model.File.InputStream.CopyToAsync(dataStream); try { using (HttpWebResponse response = (HttpWebResponse)req_uploadFile.GetResponse()) { if (response.StatusCode == HttpStatusCode.OK) { return(true); } } } catch (Exception el) { throw el; } return(false); }
public async Task<bool> UploadFileAsync(Graph.GraphService graphService, UploadFileModel model) { var graphToken = AuthenticationHelper.GetGraphAccessTokenAsync(); var propertyGroup = await graphService.GetGroupByDisplayNameAsync(model.PropertyTitle); if (propertyGroup == null) return false; string id = string.Empty; string requestUri = string.Format("{0}{1}/groups/{2}/files", AADAppSettings.GraphResourceUrl, AppSettings.DemoSiteCollectionOwner.Split('@')[1], propertyGroup.objectId); //create placeholder HttpWebRequest reqCreatePlaceholder = (HttpWebRequest)HttpWebRequest.Create(requestUri); reqCreatePlaceholder.Method = "POST"; reqCreatePlaceholder.Headers.Add("Authorization", await graphToken); reqCreatePlaceholder.ContentType = "application/json"; string content = string.Format("{{type:'File',name:'{0}'}}", model.File.FileName); byte[] contentBody = System.Text.ASCIIEncoding.UTF8.GetBytes(content); using (Stream dataStream = reqCreatePlaceholder.GetRequestStream()) { dataStream.Write(contentBody, 0, contentBody.Length); } try { using (HttpWebResponse response = (HttpWebResponse)reqCreatePlaceholder.GetResponse()) { if (response.StatusCode == HttpStatusCode.Created) { string responseString = (new StreamReader(response.GetResponseStream())).ReadToEnd(); id = Newtonsoft.Json.Linq.JObject.Parse(responseString)["id"].ToString(); } } } catch { throw; } if (string.IsNullOrEmpty(id)) return false; //upload file HttpWebRequest req_uploadFile = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}/{1}/uploadContent", requestUri, id)); req_uploadFile.Method = "POST"; req_uploadFile.Headers.Add("Authorization", await graphToken); using (var dataStream = req_uploadFile.GetRequestStream()) await model.File.InputStream.CopyToAsync(dataStream); try { using (HttpWebResponse response = (HttpWebResponse)req_uploadFile.GetResponse()) { if (response.StatusCode == HttpStatusCode.OK) { return true; } } } catch (Exception el) { throw el; } return false; }
public async Task<ActionResult> UploadFile(UploadFileModel model) { if (model.File != null) { var token = AuthenticationHelper.GetAccessTokenAsync(AppSettings.DemoSiteServiceResourceId); var graphService = AuthenticationHelper.GetGraphServiceAsync(); var dashboardService = new Dashboard(await token); await dashboardService.UploadFileAsync(await graphService, model); } return RedirectToAction("InspectionDetails", new { id = model.IncidentId }); }
public async Task<bool> UploadFileAsync(GraphServiceClient graphService, UploadFileModel model) { try { var graphToken = AuthenticationHelper.GetGraphAccessTokenAsync(); var propertyGroup = await graphService.GetGroupByDisplayNameAsync(model.PropertyTitle); if (propertyGroup == null) return false; DriveItem driveItem = new DriveItem(); driveItem.Name = model.File.FileName; driveItem.File = new Microsoft.Graph.File(); driveItem = await graphService.Groups[propertyGroup.Id].Drive.Root.Children.Request().AddAsync(driveItem); await graphService.Groups[propertyGroup.Id].Drive.Items[driveItem.Id].Content.Request().PutAsync<DriveItem>(model.File.InputStream); } catch (Exception el) { throw el; } return false; }