从System.Io.Stream类C#asp.net下载文件

时间:2018-04-13 12:35:51

标签: c# asp.net download microsoft-graph filestream

我正在尝试使用microsoft graph api从我的onedrive下载文件。我已经设法达到了可以从GraphServiceClient中检索项目的程度。但是,一旦我使用/me/Drive/Items/{itemid}/Content获取文件,它就会返回System.IO.Stream个对象。

如何使用此System.IO.Stream对象下载文件?

以下是我现在获取文件的代码(出于安全考虑,我没有将itemid放在示例代码中,但我可以向您保证它是正确的。):

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 });
    }
}

此外,我想创建一个可以在视图中执行此下载的按钮。

0 个答案:

没有答案