# 浏览器配置与 OLE 附件

- Human documentation: [https://docs.univer.ai/zh-CN/server/import-export/browser](https://docs.univer.ai/zh-CN/server/import-export/browser)

- Agent Markdown: [https://docs.univer.ai/zh-CN/server/import-export/browser.md](https://docs.univer.ai/zh-CN/server/import-export/browser.md)

- Requested language: `zh-CN`

- Content language: `zh-CN`

- Documentation version: `1.6.2`

- Source: [import-export/browser.zh-CN.mdx](https://github.com/dream-num/documentation/blob/dev/content/server/import-export/browser.zh-CN.mdx)

---

## 插件配置 [#exchange-client-configuration]

`UniverExchangeClientPlugin`

| 配置项 | 类型 | 默认值 | 作用 |
| --- | --- | --- | --- |
| `downloadEndpointUrl` | `string` | `location.origin` | 下载文件的基础地址。 |
| `uploadFileServerUrl` | `string` | `'/universer-api/stream/file/upload'` | 接收上传文件的接口。 |
| `importServerUrl` | `string` | `'/universer-api/exchange/{type}/import'` | 启动导入任务的接口。保留 `{type}` 占位符。 |
| `exportServerUrl` | `string` | `'/universer-api/exchange/{type}/export'` | 启动导出任务的接口。保留 `{type}` 占位符。 |
| `getTaskServerUrl` | `string` | `'/universer-api/exchange/task/{taskID}'` | 查询任务状态的接口。保留 `{taskID}` 占位符。 |
| `signUrlServerUrl` | `string` | `'/universer-api/file/{fileID}/sign-url'` | 为文件 URL 签名的接口。保留 `{fileID}` 占位符。 |
| `maxTimeoutTime` | `number` | `120000` | 轮询转换任务的最长时间，单位为毫秒。 |

## OLE 附件 [#ole-attachments]

Sheets、Docs 和 Slides 会将导入的 OLE 原文件与预览图分开保存。原文件可用时，用户可以下载，也可以重命名某个附件实例；拥有权限时还可以删除附件。图片和 PDF 附件可以预览。Office 预览通过 Exchange 的 JSON 转换，在临时的只读编辑器中打开，不会生成持久化的子文档。原生 Office 应用和宏不会被执行。

将 Office 附件转换为在线文档，需要宿主支持协同、具备编辑权限、支持转换，并且已配置 `getLink`。转换会新建一份独立文档，将其标识记录在附件上，之后打开时复用这份文档。编辑该在线文档不会覆盖附件的原始字节。缺失、外部引用或不支持的附件会保留原状态，不会变成可编辑的 Office 对象。

导出默认使用 `oleAttachmentSource: 'original'`。设为 `'online'` 时，只在这一个导出文件中嵌入各在线文档当前已确认的版本，存储的原件不会被改写。导出前必须等待同步完成，后端应校验被引用文档的访问权限。Docs 和 Slides 的导出 API 将该选项作为第二个参数，Sheets 作为第四个参数；snapshot 变体的参数位置相同。参见[导出 API](https://docs.univer.ai/zh-CN/reference/facade/univer.md#exportsheetbyunitidasync)。

对于已持久化的附件，Exchange 会提交 `fileID` 和拥有该附件的 `sourceUnitID`。导入后端在按引用转换之前，必须先校验两者的归属关系以及调用者的权限。轮询任务时可能返回 `progress.stage`，其值为 `reading`、`converting`、`creating` 或 `done`。自定义转换界面见[附件导入与进度 API](https://docs.univer.ai/zh-CN/reference/packages/plugins/univerjs-pro/exchange-client.md#attachment-imports-and-task-progress)。

## 配置 OLE 附件 [#ole-configuration]

`UniverDrawingPlugin`

| 配置项 | 类型 | 默认值 | 作用 |
| --- | --- | --- | --- |
| ole | `object` |  | OLE 附件的配置项。 |
| getLink | `IOleConfig["getLink"]` |  | 生成打开在线附件的应用链接。 |
| attachmentIo | `object` |  | 在应用自己的存储里读写附件原文件。 |
| upload | `(file: File, targetUnitId: string) => Promise<IBaseAttachment>` |  | 把附件写入应用存储，并关联到当前单元。 |
| read | `(attachment: IBaseAttachment, signal?: AbortSignal) => Promise<Uint8Array>` |  | 从应用存储读取附件字节。 |
| previewOffice | `IOleConfig["previewOffice"]` |  | 接管 Office 附件的预览流程。 |
| createOfficePreview | `IOleConfig["createOfficePreview"]` |  | 用应用提供的回调创建 Office 预览。 |

注册 Exchange 之前，先将 `ole` 加入已有的 `UniverDrawingPlugin` 注册项。下面的路由由应用提供，需要指向应用自己的文档界面；需要文档类型时，通过 `getLink` 的第二个参数传入。Exchange 负责 Office 转换，也会检测协同状态。以 UUID 引用的附件还需要配置 `ole.attachmentIo`，提供带鉴权的 `upload(file, targetUnitId)` 和 `read(attachment, signal)` 回调。Office 预览需要对应的渲染器，或由应用实现 `previewOffice` / `createOfficePreview` 回调。

```ts
import { UniverDrawingPlugin } from '@univerjs/drawing'

univer.registerPlugin(UniverDrawingPlugin, {
  ole: {
    getLink: (unitId) => `/documents/${encodeURIComponent(unitId)}`,
  },
})
```
