大网数据广告活动
Microsoft Graph 接口

上传小文件

· 52CE 我爱测

通过简单上传 API,可以在一个 API 调用中提供新文件的内容或更新现有文件的内容。

命名空间:microsoft.graph

通过简单的上载 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。

HTTP

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

The contents of the file goes here.

C#


GraphServiceClient graphClient = new GraphServiceClient( authProvider );

using var stream = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(@"The contents of the file goes here."));

await graphClient.Me.Drive.Items["{driveItem-id}"].Content
    .Request()
    .PutAsync<DriveItem>(stream);

有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档。

JavaScript


const options = {
    authProvider,
};

const client = Client.init(options);

const stream = The contents of the file goes here.;

await client.api('/me/drive/items/{item-id}/content')
    .put(stream);

有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档。

Java


GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();

byte[] stream = Base64.getDecoder().decode("The contents of the file goes here.");
    graphClient.me().drive().items("{item-id}")
    .buildRequest()
    .put(stream);

有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档。


响应

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

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

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

错误响应

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

历史资料。此文对应 2023-02-03 的中文文档快照,适用产品版本和命令行为以原文为准。

来源与署名:Microsoft 和文档贡献者(原始作者元数据 JeremyKelley)。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。

资料来源与适用范围

资料来源
Microsoft / Microsoft Graph 中文文档历史快照
适用范围
历史中文快照 2023-02-03;详见原文产品版本;文档日期 09/10/2017
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
CC BY 4.0(文档)/ MIT(示例代码)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。