# file.update()

Canonical URL: https://developers.iagenify.com/zh/sdk/ref/files/update/
Language: zh

`file.update() → Promise<Result>`

替换已保存文件的内容。不需要任何参数:它现在发送对象上的内容。

Status: PUT /api/files/:id · free · needs a sourceId

不寻常的部分是它读的是对象,而不是其论点。 `file.content`调,调 `update()`,这就是新的内容。

```javascript
const file = await api.files.get(sourceId);

file.content = "# Q4\n\nRevised after the audit.";
await file.update();
```

## 需要的东西 {#requires}

| 条件条件 | 否则 |
| --- | --- |
| file.sourceId 设置 | 排 SDK 扔弃:没有要更新的 |
| file.content 并非空 | 排 SDK 扔弃:空文件不是内容 |

两者在提出要求之前都被扔到当地。 `sourceId` 调自 [(c) () 获得](/zh/sdk/ref/files/get)或获得它 [(已)](/zh/sdk/ref/files/save) 自然序列运作 新鲜 `create()`d 对象没有。

```javascript
const file = api.files.create({ name: "notes.md", content: "…" });
await file.update();   // throws — never saved, so no sourceId

await file.save();     // now it has one
file.content = "…";
await file.update();   // fine
```

## 返回值 {#returns}

```javascript
{
  ok: true,
  message: "…",
  sourceId: "…",
  metadata: { size: 2048, mimeType: "text/markdown" }
}
```

## 替换,不附加 {#replaces}

新内容是整个内容。 要添加到文件、 读取、 连接和更新 。

```javascript
const file = await api.files.get(sourceId);
file.content = `${file.content}\n${newLine}`;
await file.update();
```

> **没有上一个版本**
>
> 旧内容已经删除。 如果重要的话, 先读取它—— 读取是免费的。 历史重要文件需要每个版本的新文件, 而不是更新 。

## 事物不变, {#immutable}

| 变动 | 未修改 |
| --- | --- |
| 内容及其大小 | 名称名称 |
| 仓库剩余空间 | MIMEType, 所以分类 |
|  | 源Id |

因此文本文件不能通过更新而成为图像。 另存一个新的文本文件 。

## 可搜索文字没有重新索引 {#indexing}

> **语义搜索仍然来自旧文本的答案**
>
> 保存时会使用索引。更新后,搜索可以返回您替换的版本。如果重要,删除并保存,而不是更新。

## 错误 {#errors}

| 代码 | 状态 | 触发条件 |
| --- | --- | --- |
| —（在本地抛出） | — | 没有源Id, 也没有内容 。 |
| file_not_found | 404 | 此账户没有此id 的文档 。 |
| missing_content | 400 | 没有可用的设备到达服务器 。 |

- [get()](/zh/sdk/ref/files/get): 正在读取 。 源代码Id 已设置 。
- [delete()](/zh/sdk/ref/files/delete): 移除它, 和从它索引的东西。

```python
file = api.files.get(source_id)

file.content = "# Q4\n\nRevised after the audit."
file.update()
```

```python
file = api.files.create(name="notes.md", content="…")
file.update()   # raises ValueError — never saved, so no source_id

file.save()     # now it has one
file.content = "…"
file.update()   # fine
```

```python
{
    "ok": True,
    "message": "…",
    "sourceId": "…",
    "metadata": {"size": 2048, "mimeType": "text/markdown"},
}
```

```python
file = api.files.get(source_id)
file.content = f"{file.content}\n{new_line}"
file.update()
```
