# file.save()

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

`file.save({ storageId, path, localOnly }) → Promise<Result>`

将文件写入账户, 本地磁盘, 或者两者都写入。 这里的通话需要信用。

Status: POST /api/files/save · 1 credit · refunded if the write fails

## 参数 {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `storageId` | string |  | 要写入哪个存储。 没有它, 账户违约 。 |
| `path` | string |  | Node 仅此。同时将字节写入本地路径,创建文件夹。 |
| `localOnly` | boolean |  | 与 `path`:在本地和 **停止停止**。没有上载任何信息,也没有指控。 |

## 使用它的三个方法 {#three}

```javascript
// 1. To the account — the usual one
await file.save();

// 2. To the account AND to this machine, in one call
await file.save({ path: "./out/report.md" });

// 3. To this machine only — no request, no credit
await file.save({ path: "./out/report.md", localOnly: true });
// { ok: true, status: "saved_local", path: "./out/report.md" }
```

> **本地写作失败 本地签名无法失败**
>
> 如果路径无法写入, 则 SDK 在控制台上警告并直接上传。 当本地副本是重要的时, 请自己检查文件—— 仅读文件夹不会上升 。

## 返回值 {#returns}

```javascript
{
  ok: true,
  status: "saved",
  sourceId: "…",            // how you find it again
  name: "report.md",
  category: "document",
  mimeType: "text/markdown",
  location: "object",       // the kind of storage it landed in
  metadata: {
    size: 2048,
    storageId: "…",
    remainingBytes: null,   // what is left, when the storage tracks it
    durationMs: 260
  },
  creditsUsed: 1
}
```

物体从中学习: `file.sourceId`, `file.location` 和 `file.metadata` 设置,这是它制造 [(更新)](/zh/sdk/ref/files/update) 之后可以直接进行。

## 选择它去到哪里 {#target}

```javascript
// The account's default
await file.save();

// A storage you named
const all = await api.storage.list();
const archive = all.find((s) => s.label === "Archive");
await file.save({ storageId: archive.id });
```

| 储存种类 | 保存到它 |
| --- | --- |
| 对象 □ 数据库 | 是,-账户上保留字节 |
| 文件系统( 计算机) | 拒绝: storage_is_local |
| 电话 浏览器 | 拒绝: storage_is_local |

拒绝的来来 **之前** 信用已经用完。 这不是一次不重复的尝试: 那些设备保存着自己的文件, 写它们的方法来自设备 。

## 功劳,当它回来的时候 {#cost}

- 写作前先记一个分数
- 如果写作失败,它就是 **全额退还** 并且你得到 `502 storage_failed`.
- 首先出现的拒绝——没有储存、当地储存、没有内容——根本不花费任何费用。

## 得到回复后,文件正在不断整理 {#indexing}

当类别是 `document`,文本在回复发送后就索引化为语义搜索索引。因此,文件可以立即读取,并在稍后某个时刻逐个查找。

> **你不要在同呼吸中寻找你刚刚保存的东西**
>
> 刻意不等待索引——它能迅速保存,随后立即进行搜索可能还没有看到。

## 错误 {#errors}

| 代码 | 状态 | 触发条件 | 信贷信贷信贷信贷信贷 |
| --- | --- | --- | --- |
| —（在本地抛出） | — | 内容未定义或无效。 | 无 |
| missing_file_content | 400 | 服务器上既没有文件也没有到达文本内容 。 | 无 |
| storage_not_found | 404 | 该账户根本没有存储。 | 无 |
| storage_is_local | 400 | 目标是一台电脑或一部电话 | 无 |
| not_enough_credits | 402 | 余额不会弥补。 | 无 |
| storage_failed | 502 | 写作本身失败了 。 | 已退还退退 |

```javascript
import { IaGenifyError } from "iagenify";

try {
  await file.save({ storageId });
} catch (err) {
  if (err instanceof IaGenifyError && err.code === "storage_is_local") {
    // That storage is a device. Write it there instead.
    const device = await api.storage.get({ id: storageId });
    await device.files.write("report.md", file.content);
  } else throw err;
}
```

- [file.update()](/zh/sdk/ref/files/update): 之后取代内容。
- [本地文件](/zh/sdk/ref/local-files): 改写机器自己的文件
- [额度](/zh/sdk/concepts/credits): 如何花和还钱。

```python
# 1. To the account — the usual one
file.save()

# 2. To the account AND to this machine, in one call
file.save(path="./out/report.md")

# 3. To this machine only — no request, no credit
file.save(path="./out/report.md", local_only=True)
# {"ok": True, "status": "saved_local", "path": "./out/report.md"}
```

```python
{
    "ok": True,
    "status": "saved",
    "sourceId": "…",            # how you find it again
    "name": "report.md",
    "category": "document",
    "mimeType": "text/markdown",
    "location": "object",       # the kind of storage it landed in
    "metadata": {
        "size": 2048,
        "storageId": "…",
        "remainingBytes": None,   # what is left, when the storage tracks it
        "durationMs": 260,
    },
    "creditsUsed": 1,
}
```

```python
# The account's default
file.save()

# A storage you named
archive = next(s for s in api.storage.list() if s.label == "Archive")
file.save(storage_id=archive.id)
```

```python
from iagenify import IaGenifyError

try:
    file.save(storage_id=storage_id)
except IaGenifyError as error:
    if error.code != "storage_is_local":
        raise
    # That storage is a device. Write it there instead.
    device = api.storage.get(id=storage_id)
    device.files.write("report.md", file.content)
```
