【发布时间】:2021-08-17 23:28:10
【问题描述】:
我有一个 Blazor Server 应用程序,它要求能够允许用户从其本地文件系统中选择一个文件并将其上传到 SharePoint Online 库中。 我正在将 PnP.Core 包用于 SharePoint 功能。
应用程序显示一个文件选择对话框,允许用户选择特定文件。当我尝试将文件添加到 SPO 库时,我收到 401 错误。我理解为什么会发生这种情况,但我不知道如何正确添加文件。
相关的sn-p代码在这里:
/// <summary>
/// Upload file from local file system to SPO library
/// </summary>
/// <param name="fileToUpload">File selected by user</param>
/// <param name="pnpContextFactory">PnP Context Factory</param>
/// <returns></returns>
public async Task<IActionResult> AddAttachment(IBrowserFile fileToUpload, PnPContextFactory pnpContextFactory)
{
// Create new PnPContext from factory
using (var context = await pnpContextFactory.CreateAsync("SiteToWorkWith"))
{
// target location on SPO
string documentUrl = $"{context.Uri.PathAndQuery}/Documents/PQI";
// get folder object from PnPContext
var folder = await context.Web.GetFolderByServerRelativeUrlAsync(documentUrl);
if (folder != null)
{
// Open a stream (for testing, this would normally be combined in the next line
Stream s = fileToUpload.OpenReadStream();
// Upload the file
// This is where the 401 error is thrown
await folder.Files.AddAsync(fileToUpload.Name, s);
}
}
return View();
}
我相当确信该错误是由浏览器安全引起的,因此我正在寻找替代解决方案。
【问题讨论】:
标签: sharepoint file-upload server blazor