# create()

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

`api.files.create({ name, content, mimeType }) → IaGenifyFile`

在内存中构建文件对象。没有发送任何文件,没有写入任何文件,没有充电——这是所有这一切之前的步骤。

Status: no network · synchronous · returns an object to fill

## 参数 {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `name` | string |  | 文件名。 账户上使用的原样; 服务器将它降为自有磁盘上的安全文件 。 |
| `content` | string|Buffer|Blob |  | 字节。 可以在建筑后设置 - 读取于 `save()`而不是在这里 |
| `mimeType` | string |  | 默认默认为 `application/octet-stream`类别,即类别 `document`. |

它同步返回,没有 `await`:这是一个建筑师,名称除外。

## 对象 {#returns}

```javascript
const file = api.files.create({ name: "notes.md", content: "…", mimeType: "text/markdown" });

file.name;
file.content;
file.mimeType;
file.sourceId;   // null until it is saved
file.location;   // null until it is saved
file.metadata;   // null until it is saved
```

## 设置 MIMET 类型, 即使它是可选的 {#mime}

它可以决定两件事 以后改变是尴尬的 [类别](/zh/sdk/ref/files),以及是否 `get()` 内容作为文本或字节以文字或字节形式重新掌起。

| 你过 | 类别 | 返回 () 返回 |
| --- | --- | --- |
| 文字文字文字文字/markdown | 文档文档文档文档 | 字符串内容内容 |
| 应用/json | 数据数据数据数据数据 | 字符串内容内容 |
| 图像图像图像图像/png | 图像图像图像图像 | 内装为缓冲 |
| (无) | 文档文档文档文档 | 内装为缓冲 |

> **离开它,文字就会以字节的形式回来**
>
> 没有类型, 默认是一个二进制文件, 所以一个完全可读的 Markdown 文件会以缓存形式被交还。 现在有一条工作线, 或者稍后会读错 。

## 接受内容 {#content}

```javascript
// A string
api.files.create({ name: "notes.md", content: "# Notes", mimeType: "text/markdown" });

// Bytes you already have
api.files.create({ name: "chart.png", content: pngBuffer, mimeType: "image/png" });

// Something generated — write it out the moment you have it
const { outputs } = await assistant.generate({ inputs, outputs: [{ type: "image" }] });
api.files.create({ name: "cover.png", content: outputs[0].content, mimeType: outputs[0].mime });
```

## 之后填填 {#later}

保存时内容会读取, 所以先构建对象, 然后再填充它是好的, 并且它是如何 `update()` 工作。

```javascript
const file = api.files.create({ name: "log.txt", mimeType: "text/plain" });

file.content = lines.join("\n");
await file.save();

// And to replace it later
file.content = "…";
await file.update();
```

> **本地拒绝保存无内容的保存**
>
> 下 SDK 当内容未定义或无效时, 将丢弃在任何请求之前—— 空字符串则是一个合法的空文件 。

- [file.save()](/zh/sdk/ref/files/save): 哪里变成现实
- [输出类型](/zh/sdk/concepts/outputs): 将媒体从结果中生成, 并进入文件 。

```python
file = api.files.create(name="notes.md", content="…", mime_type="text/markdown")

file.name
file.content
file.mime_type
file.source_id   # None until it is saved
file.location    # None until it is saved
file.metadata    # None until it is saved
```

```python
import base64

# A string
api.files.create(name="notes.md", content="# Notes", mime_type="text/markdown")

# Bytes you already have
api.files.create(name="chart.png", content=png_bytes, mime_type="image/png")

# Something generated — write it out the moment you have it
outputs = assistant.generate(inputs=inputs, outputs=[{"type": "image"}])["outputs"]
api.files.create(name="cover.png", content=base64.b64decode(outputs[0]["content"]), mime_type=outputs[0].get("mime"))
```

```python
file = api.files.create(name="log.txt", content="", mime_type="text/plain")

file.content = "\n".join(lines)
file.save()

# And to replace it later
file.content = "…"
file.update()
```
