I am trying to download a file from my onedrive using the microsoft graph api. I have managed to get to the point where i can retrieve the item from the GraphServiceClient. However, once i get the file using /me/Drive/Items/{itemid}/Content
it returns to me a System.IO.Stream
object.
How do i use this System.IO.Stream
object to download the file?
Here is the code i have now for getting the file (for security purposes i did not put the itemid in the sample code, but i can assure you it is correct.) :
public async Task<ActionResult> OneDriveDownload()
{
string token = await GetAccessToken();
if (string.IsNullOrEmpty(token))
{
// If there's no token in the session, redirect to Home
return Redirect("/");
}
GraphServiceClient client = new GraphServiceClient(
new DelegateAuthenticationProvider(
(requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
return Task.FromResult(0);
}));
try
{
var stream = await client.Me.Drive.Items[ItemID].Content.Request().GetAsync();
return View(stream);
}
catch (ServiceException ex)
{
return RedirectToAction("Error", "Home", new { message = "ERROR retrieving messages", debug = ex.Message });
}
}
Additionally, i would like to create a button that can execute this download in the view.