# write()

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/local-files/write/
Language: zh

`files.write(file, content) → Promise<Item>`

写入文件和返回已写的内容。文本或二进制,亲子创建,替换现有文件。

Status: free · replaces · text and binary

## 参数 {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `file` | string | 必填 | 相对于工作空间的写入路径 。 |
| `content` | string | Buffer | Uint8Array | ArrayBuffer | 必填 | 写什么 其它的都被拒绝了 |

## 文本 {#text}

```javascript
const item = await pc.files.write("Launch plan/brief.md", "# Brief\n\nShip in March.");
// { name: "brief.md", path: "Launch plan/brief.md", type: "file", size: 29,
//   mimeType: "text/markdown", modifiedAt: "…" }
```

字符串以 UTF-8 -8 -8 -2 `mimeType` 返回的文件从扩展名中读取,而不是从内容中读取,所以您选择的名称是决定文件后来如何处理的名称。

## 二进二进 {#binary}

```javascript
// A generated image, straight from a model session
const [picture] = await session.generate({
  inputs: [{ type: "text", content: "A fox in the snow" }],
  outputs: [{ type: "image" }],
}).then((r) => r.outputs);

await pc.files.write("Assets/fox.png", Buffer.from(picture.content, "base64"));

// A download
const response = await fetch("https://example.com/report.pdf");
await pc.files.write("Reports/report.pdf", await response.arrayBuffer());
```

## 它可以代替它,它一无所存,一无所存 {#replaces}

> **这里没有上一个版本**
>
> 平面操作 : 旧内容已经删除。 保存副本是代理商自己的写工具在. versions/ 上方添加的内容, 如果您想要在代码中出现该行为, 请先读取文件, 然后自己写出副本 。

```javascript
// Keep a copy before replacing
const before = await pc.files.read("notes.md");
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
await pc.files.write(`.versions/notes.md/${stamp}.md`, before.content);
await pc.files.write("notes.md", next);
```

## 需要时创建文件夹 {#parents}

写书 `Clients/Acme/2026/invoice.md` 沿途创建三个文件夹。 不论深度如何, 都有一个呼叫 。

## 错误 {#errors}

| 代码 | 状态 | 触发条件 |
| --- | --- | --- |
| missing_content | 400 | `content` 是 `undefined` 或 `null`。空字符串是有效的。 |
| invalid_content | 400 | 不是弦,不是缓冲,不是Uint8阵列,也不是阵列。 |
| invalid_path | 400 | 绝对,在工作空间之外,或者工作空间根之外。 |
| local_files_require_node | 400 | 运行环境没有文件系统。 |
| storage_not_on_this_machine | 409 | 另一台电脑的储藏室 |

对象为 **否** 给您序列序列的号码: 通行证 `JSON.stringify(value)`,所以格式 保持你的。

- [read()](/zh/sdk/ref/local-files/read): 读回去——文字是字符串,二进制是缓冲。
- [本地文件](/zh/sdk/ref/local-files): 工作空间、 路径规则、 以及每个方法返回的形状 。

```python
item = pc.files.write("Launch plan/brief.md", "# Brief\n\nShip in March.")
# {"path": "Launch plan/brief.md", "size": 29}
```

```python
import base64, urllib.request

# A generated image, straight from a model session
picture = session.generate(
    inputs=[{"type": "text", "content": "A fox in the snow"}],
    outputs=[{"type": "image"}],
)["outputs"][0]

pc.files.write("Assets/fox.png", base64.b64decode(picture["content"]))

# A download
with urllib.request.urlopen("https://example.com/report.pdf") as response:
    pc.files.write("Reports/report.pdf", response.read())
```

```python
from datetime import datetime, timezone

# Keep a copy before replacing
before = pc.files.read("notes.md")
stamp = datetime.now(timezone.utc).isoformat().replace(":", "-").replace(".", "-")
pc.files.write(f".versions/notes.md/{stamp}.md", before["content"])
pc.files.write("notes.md", next_content)
```
