【问题标题】:Microsoft Graph Api Upload Large File in a folderMicrosoft Graph Api 在文件夹中上传大文件
【发布时间】:2020-09-15 20:56:59
【问题描述】:

我正在尝试使用 LargFileUploadTask 将大于 4 mb 的文件上传到 Sharepoint 列表中的文件夹中。该代码似乎在“城市”文件夹中的 PostAsync 之后创建临时文件。我看到创建了一个文件,即 ~tmphamilton.png 。但是当 UploadAsync 被调用时,它会失败,并返回“找不到资源”

当我在创建 uploadSession 时删除文件夹路径时,代码可以正常工作

知道我做错了什么吗?

   // create an upload session
var uploadSession = await graphClient.Sites("site id").Drive().Root().ItemWithPath("cities\hamilton.png").CreateUploadSession().Request().PostAsync();

var maxSliceSize = 320 * 1024; // 320 KB - Change this to your slice size. 5MB is the default.

var largeFileUploadTask = new LargeFileUploadTask(uploadSession, stream, maxSliceSize);

// upload away with relevant callback
DriveItem itemResult = await largeFileUploadTask.UploadAsync( progress );

【问题讨论】:

  • 您是否确保同名文件的先前会话已关闭/过期。当上传的某些方面失败时,我也遇到过类似的问题,然后在 4 小时内没有进一步的上传可以工作?直到失败(未关闭)的会话过期。

标签: c# sharepoint microsoft-graph-api


【解决方案1】:

确保最大值。您的 WebApplication 中的上传大小设置大于您的文件。取决于上传位置和 SharePoint 版本,它可能少至 50MB。在 Premise 中,可以在 CentralAdmin->Application Management->Web Application xy->General Settings

进行设置

也检查https://docs.microsoft.com/de-de/archive/blogs/bgeoffro/list-attachments-over-50mb-need-more-than-an-increase-in-maximum-upload-size

【讨论】:

    【解决方案2】:

    您能否成功将文件上传到文档库的根目录?请参阅下面的代码。

    string siteId = WebConfigurationManager.AppSettings.Get("SiteId");
    string libraryName = WebConfigurationManager.AppSettings.Get("LibraryName");
    string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
    
    var uploadSession = await graphServiceClient.Sites[siteId].Lists[libraryName].Drive.Root
    .ItemWithPath(fileName)
    .CreateUploadSession(uploadProps)
    .Request()
    .PostAsync();
    

    【讨论】:

    • 是的,它可以找到上传到根目录,但不能上传到文件夹,即 ItemWithPath("hamilton.png") 有效,但 ItemWithPath("cities\hamilton.png") 无效
    【解决方案3】:

    它确实适用于 Graph 3.1.4 和 Java,我花了一段时间才让它工作,但它上传文件 >4MB 的性能非常好......

    // itemPath 类似于“/myFolder/DailyUpload/”

      public CompletableFuture UploadFileToSharePoint(String idSite, String parentId, String itemPath, String fileName, InputStream file) {
        final List<Option> options = new LinkedList<Option>();
        try {
          return  mClient
                    .sites(idSite)
                    .drive()
                    .root()
                    .itemWithPath(itemPath+ Build.ID.toUpperCase()+"/"+fileName)
                    .createUploadSession(
                            DriveItemCreateUploadSessionParameterSet.newBuilder()
                            .withItem(new DriveItemUploadableProperties())
                            .build())
                    .buildRequest(options)
                    .postAsync().thenCompose(uploadSession -> {
    
                byte[] data = null;
                try {
                        byte[] buff = new byte[file.available()];
                        int bytesRead = 0;
                        ByteArrayOutputStream bao = new ByteArrayOutputStream();
                        while ((bytesRead = file.read(buff)) != -1) {
                            bao.write(buff, 0, bytesRead);
                        }
                        data = bao.toByteArray();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
    
                if (data != null && data.length>0) {
                    final InputStream uploadFile = new ByteArrayInputStream(data); //"{\"hehe\":\"haha\"}".getBytes(StandardCharsets.UTF_8));
                    try {
                        final long fileSize = (long) uploadFile.available();
                        LargeFileUploadTask<DriveItem> largeFileUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession,
                                mClient, uploadFile, fileSize, DriveItem.class);
                        // If everything is fine, the procedure returns a DriveItem with the item already
                        return largeFileUploadTask.uploadAsync();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                return null;
            });
        }
        catch (IndexOutOfBoundsException e) {
            Log.d("GraphHelper", "UploadFileToSharePoint failed: "+ e.getMessage());
            return CompletableFuture.completedFuture(false);
        }
    }
    

    希望这对其他人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多