---
title: "受管联网与文件传输"
description: "通过 Host 配置的逻辑服务执行 GET、持久写入和文件上传下载。"
---

> Documentation Index
> Fetch the complete documentation index at: https://h5-app-docs.glass.tengyusmart.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 受管联网与文件传输

应用在 `app.json` 的 `services` 中声明平台分配的 `{ id, contractVersion }`。向平台确认允许的方法、路径、响应结构、身份要求及联网路由。Host 配置服务来源和认证，H5 传入 `serviceId` 与约定路径。

## 读取与写入

| 任务 | 入口 | 应用权限 |
| --- | --- | --- |
| 读取 JSON | `glass.http.request({ serviceId, path, method: 'GET' }).result` | `network.http` |
| 写入业务数据 | `glass.operations.enqueue({ type: 'service.write', idempotencyKey, serviceId, path, method, body })` | `network.http`、`operations.durable` |
| 下载文件 | `glass.files.download({ serviceId, path }).result` | `network.http`、`files.transfer` |
| 上传文件 | `glass.files.upload({ fileId, serviceId, path, idempotencyKey })` | `network.http`、`files.transfer`、`operations.durable` |

普通 HTTP 入口使用 GET。写入支持 POST、PUT、PATCH、DELETE，先保存固定请求与幂等键，再按 [持久操作](https://h5-app-docs.glass.tengyusmart.cn/reference/operations/) 执行。HTTP 非 2xx 也可作为响应返回，使用 `status` 和服务契约判断结果。

```ts
import { glass } from '@glass/bridge';

export async function readJson(serviceId: string, path: string) {
  await glass.ready();
  const response = await glass.http.request({ serviceId, path, method: 'GET' }).result;
  if (response.status < 200 || response.status >= 300) {
    throw new Error(`服务读取失败，HTTP ${response.status}`);
  }
  return response.body;
}
```

## 传输状态与连接

文件字节由 Host 传输。下载成功返回 `fileId`、大小、摘要及 MIME 等元数据。上传成功返回服务契约中的 `remoteId`。上传使用受管文件，后端上传方式由对接契约决定。Task 或持久操作的进度应来自实际字节；总量未知时仅显示已完成字节。最终结果从 Task Promise 或操作快照取得。

普通服务可按环境使用眼镜直连或手机中继。手机已连接、已启用中继和服务可达是三个独立状态。读取失败时可先检查 `glass.system.getConnection()` 和 `glass.network.getStatus()`，等连接恢复后重新读取。原写入或上传的结果未知时，查询原操作。使用平台业务账号的服务另有会话和权限要求，接入方式由平台确认。

Source: https://h5-app-docs.glass.tengyusmart.cn/reference/network/index.mdx
