上传或替换 driveItem 的内容

通过简单的上载 API,你可以在单个 API 调用中提供新文件的内容,也可以更新现有文件的内容。此方法仅支持最大大小为 4 MB 的文件。

若要上传大文件,请参阅通过上传会话上传大文件

权限

要调用此 API,需要以下权限之一。要了解详细信息,包括如何选择权限的信息,请参阅权限

权限类型 权限(从最低特权到最高特权)
委派(工作或学校帐户) Files.ReadWrite、Files.ReadWrite.All、Sites.ReadWrite.All
委派(个人 Microsoft 帐户) Files.ReadWrite、Files.ReadWrite.All
应用程序 Files.ReadWrite.All、Sites.ReadWrite.All

HTTP 请求(替换现有项)

PUT /drives/{drive-id}/items/{item-id}/content
PUT /groups/{group-id}/drive/items/{item-id}/content
PUT /me/drive/items/{item-id}/content
PUT /sites/{site-id}/drive/items/{item-id}/content
PUT /users/{user-id}/drive/items/{item-id}/content

HTTP 请求(上传新文件)

PUT /drives/{drive-id}/items/{parent-id}:/{filename}:/content
PUT /groups/{group-id}/drive/items/{parent-id}:/{filename}:/content
PUT /me/drive/items/{parent-id}:/{filename}:/content
PUT /sites/{site-id}/drive/items/{parent-id}:/{filename}:/content
PUT /users/{user-id}/drive/items/{parent-id}:/{filename}:/content

请求正文

请求正文的内容应该是要上载文件的二进制流。

响应

如果成功,此方法将在新创建或更新的文件的响应正文中返回 driveItem 对象。

示例(上传新文件)

此示例将字符串“The contents of the file goes here.”上传到 登录用户的驱动器中 FolderA 下名为 FileB.txt 的文件中。

PUT /me/drive/root:/FolderA/FileB.txt:/content
Content-Type: text/plain

The contents of the file goes here.

响应

如果成功,此方法将在响应正文中返回新建文件的 driveItem 资源。

HTTP/1.1 201 Created
Content-Type: application/json

{
  "id": "0123456789abc",
  "name": "FileB.txt",
  "size": 35,
  "file": { }
}

示例(更新现有文件)

此示例将文件的内容替换为已知 ID。

PUT /me/drive/items/{item-id}/content
Content-Type: text/plain

The contents of the file goes here.

响应

如果成功,此方法将在响应正文中返回新建文件的 driveItem 资源。

HTTP/1.1 201 Created
Content-Type: application/json

{
  "id": "0123456789abc",
  "name": "FileB.txt",
  "size": 35,
  "file": { }
}

注解

被阻止的文件类型

SharePoint Server 2016 阻止将具有下面几个文件扩展名的文件上传到服务器。

  • .ashx - ASP.NET Web 处理程序文件
  • .asmx - ASP.NET Web 服务源文件
  • .json - JavaScript 对象表示法文件
  • .soap - 简单对象访问协议文件
  • .svc - Windows Communication Foundation (WCF) 服务文件
  • .xamlx - Visual Studio 工作流服务文件

请参阅 添加到列表或库的文件的类型 有关阻止的文件扩展名详细信息。

冲突解决行为

默认情况下,此 API 将替换同名的现有项。 若要自定义冲突解决行为,请将名为 @microsoft.graph.conflictBehavior 且值为 failreplacerename 的查询参数添加到请求中。

错误响应

请参阅错误响应,详细了解错误返回方式。