带有哈希(#)的文件名未上传

时间:2018-10-04 14:44:38

标签: microsoft-graph

我正在尝试使用以下代码将文件发送到OneDrive:

$uri = "/me/drive/items/$folderId/children('{$fileName}')/content";

$graph = $this->graph->create($user);
$client = $this->graph->createClient();

$item = $graph->createRequest("PUT", $uri)
    ->attachBody($fileContent)
    ->setReturnType(Model\DriveItem::class)
    ->execute($client);

如果$ fileName是类似Test.doc的东西,这很好用

但是由于某种原因,当文件名中的文件名中包含井号(#)时,我会收到错误消息:

object(Microsoft\Graph\Model\DriveItem)#1509 (1) {
  ["_propDict":protected]=>
  array(1) {
    ["error"]=>
    array(3) {
      ["code"]=>
      string(10) "BadRequest"
      ["message"]=>
      string(36) "Bad Request - Error in query syntax."
      ["innerError"]=>
      array(2) {
        ["request-id"]=>
        string(36) "ff3fe15f-b1ee-4e92-8abd-2400b1c1b5cf"
         ["date"]=>
         string(19) "2018-10-04T14:30:51"
       }
    }
  }

有人可以澄清这是错误还是实际行为(即文件名中不能包含#)

谢谢

2 个答案:

答案 0 :(得分:2)

我猜您正在使用Microsoft Graph Library for PHP,需要转义诸如#之类的特殊字符。

因此,将哈希替换为%23percent encoding)或使用rawurlencode function,如下所示:

    $fileName = rawurlencode("Guide#.docx");

    $requestUrl = "https://graph.microsoft.com/v1.0/drives/$driveId/root:/$fileName:/content";

    try {
        $item = $client->createRequest("PUT", $requestUrl)
            ->attachBody($fileContent)
            ->setReturnType(Model\DriveItem::class)
            ->execute();

    } catch (\Microsoft\Graph\Exception\GraphException $ex) {
        print $ex;
    }

答案 1 :(得分:0)

尽管文件名支持# in name,但这并不意味着产品团队会首次提供API或调整现有的API,但您使用的API可能尚未完全调整以适应最新的命名规则。因此,它现在应该是实际行为,而不是错误/或者您可以将其视为不存在的功能。 enter image description here

SharePoint开发人员 issue list 中存在一个相关问题,尽管它们不是同一个人,但建议是相同的,请投票exising feature或提交一个UserVoice上的新功能。