【问题标题】:Microsoft Graph API: PUT Request files content into binary stream, upload into SharePoint document folderMicrosoft Graph API:将请求文件内容放入二进制流,上传到 SharePoint 文件夹
【发布时间】:2021-02-10 11:57:50
【问题描述】:

问题 我正在尝试创建一个新的 docx 文件,其中包含一些文本但无法传递数据,因为没有示例如何在 MS Graph API 中上传实际文件 不知道要在正文中附加哪些变量以用于内容和二进制流。如果我注释 attachBody() 参数,它将仅通过在文档文件夹的根目录中创建一个文本文件来工作。

$graph = new Graph();
$graph->setAccessToken($accessToken);
$content = [
    'Content-Type: text/plain
        The contents of the file goes here.'
    ];
$data = $graph->createRequest('PUT', '/sites/09e0add7-675f-4694-a1d9-999be420a807/drives/b!163gCV9nlEah2Zmb5CCoBwohbFPtU9lDr3_IcXUI8qiDyOuVQMhDSohSZRBTS6YL/root:/test.docx:/content')
                ->attachBody($content)
                ->setReturnType(Model\User::class)
                ->execute();

问题已更新 将文本/文档/图像/数据转换为二进制流然后执行请求

问题更新编码文件已上传到驱动器,但打开会出现错误

在下面执行前打印查询

代码

$text_data=file_get_contents('C:\Users\Lab1-WS-5\Downloads/demo.docx');
    $encoded_text_data=base64_encode($text_data);
        // ------------------------------------------------------------------------
    $graph = new Graph();
    $graph->setAccessToken($accessToken);
    // $content = 'The contents of the file goes here.';
    // $encoded = base64_encode($content);
    $data = $graph->createRequest('PUT', '/sites/09e0add7-675f-4694-a1d9-999be420a807/drives/b!163gCV9nlEah2Zmb5CCoBwohbFPtU9lDr3_IcXUI8qiDyOuVQMhDSohSZRBTS6YL/root:/charlie.docx:/content')
                ->addHeaders(array('Content-Type' => 'text/plain'))
                ->attachBody($encoded_text_data)
                ->setReturnType(Model\User::class)
                ->execute();

当前问题使用图形 api 创建的文档未打开请求权限,而手动上传的文件则没有。

【问题讨论】:

  • 您可以尝试使用$image_data=file_get_contents('test.jpg'); $encoded_image=base64_encode($image_data); 对于文本文件,您可以通过将 Content-Type 标头设置为 text/plain 来直接使用数据
  • @Shiva-MSFTIdentity 检查我的 api 查询它创建文档但无法打开,而如果你手动上传 docx 文件它打开没有任何错误
  • 创建的文件是base64的,如果你下载的文件有base64代码也不能打开

标签: php microsoft-graph-api codeigniter-3 sharepoint-api codeigniter-4


【解决方案1】:

PS不需要使用base 64编码,因为微软在线单词将无法读取它

工作代码

$text_data=file_get_contents('C:\Users\Lab1-WS-5\Downloads/wordfile.docx');
$graph = new Graph();
$graph->setAccessToken($accessToken);
$data = $graph->createRequest('PUT', '/sites/09e0add7-675f-4694-a1d9-999be420a807/drives/b!163gCV9nlEah2Zmb5CCoBwohbFPtU9lDr3_IcXUI8qiDyOuVQMhDSohSZRBTS6YL/root:/wordfile1.docx:/content')
                ->addHeaders(array('Content-Type' => 'text/plain'))
                ->attachBody($text_data)
                ->setReturnType(Model\User::class)
                ->execute();

附言 此代码适用于我测试过 excel 和 word 的任何文件

【讨论】:

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