【问题标题】:Uploading file from Blazor Server app to SharePoint Online library将文件从 Blazor Server 应用上传到 SharePoint Online 库
【发布时间】: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


    【解决方案1】:

    我已经回答了我自己的问题。这个问题实际上不是 401 问题。 我需要将用户选择的文件复制到内存流中。一旦我这样做了,我就可以让上传工作了。

      /// <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)
                {
                    **MemoryStream ms = new MemoryStream();
                    // Open a stream (for testing, this would normally be combined in the next line
                    await fileToUpload.OpenReadStream().CopyToAsync(ms);**
                    // Upload the file
                    // This is where the 401 error is thrown
                    await folder.Files.AddAsync(fileToUpload.Name, ms);
                }
            }
            return View();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-01
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多