|
| 1 | +--- |
| 2 | +title: Files |
| 3 | +parent: /#walkthrough |
| 4 | +prev: /keyboards-and-callback-queries |
| 5 | +--- |
| 6 | + |
| 7 | +Methods that send photos, videos, documents, and other media accept a {{ "FileSource" |> t }}. A file source can be a file path, URL, file ID, `Uint8Array`, iterable of byte arrays, or readable stream. |
| 8 | + |
| 9 | +## Uploading Files |
| 10 | + |
| 11 | +Pass a file source directly to the method for the type of media you want to send. |
| 12 | + |
| 13 | +```ts |
| 14 | +// A local file path. |
| 15 | +await client.sendDocument(chatId, "./report.pdf"); |
| 16 | + |
| 17 | +// A remote file. |
| 18 | +await client.sendPhoto( |
| 19 | + chatId, |
| 20 | + new URL("https://example.com/photo.jpg"), |
| 21 | +); |
| 22 | + |
| 23 | +// Bytes already in memory. |
| 24 | +await client.sendDocument(chatId, bytes, { |
| 25 | + fileName: "data.bin", |
| 26 | +}); |
| 27 | +``` |
| 28 | + |
| 29 | +Local file paths are available only in server-side runtimes. In browsers, use a URL, byte array, iterable, or stream. |
| 30 | + |
| 31 | +## Reusing Files |
| 32 | + |
| 33 | +Received media includes a `fileId`. Pass it to a compatible send method to reuse the file without uploading it again. |
| 34 | + |
| 35 | +```ts |
| 36 | +client.on("message:photo", async (ctx) => { |
| 37 | + await ctx.replyPhoto(ctx.msg.photo.fileId); |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +Media also includes a `fileUniqueId`. It remains stable and is useful for comparing files, but it cannot be used to download or resend them. |
| 42 | + |
| 43 | +## Downloading Files |
| 44 | + |
| 45 | +The `download` method returns an async generator that yields the file as chunks of bytes. |
| 46 | + |
| 47 | +```ts |
| 48 | +for await (const chunk of client.download(fileId)) { |
| 49 | + // Write or otherwise process the chunk. |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Processing chunks as they arrive avoids loading the entire file into memory. You can also set the chunk size or cancel a download with an abort signal. |
| 54 | + |
| 55 | +```ts |
| 56 | +const controller = new AbortController(); |
| 57 | + |
| 58 | +for await (const chunk of client.download(fileId, { |
| 59 | + chunkSize: 256 * 1024, |
| 60 | + signal: controller.signal, |
| 61 | +})) { |
| 62 | + // ... |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Upload Progress |
| 67 | + |
| 68 | +To receive progress updates for an upload, get a progress ID and pass it to the send method. |
| 69 | + |
| 70 | +```ts |
| 71 | +const progressId = await client.getProgressId(); |
| 72 | + |
| 73 | +client.on("uploadProgress", (ctx) => { |
| 74 | + const progress = ctx.update.uploadProgress; |
| 75 | + if (progress.id === progressId) { |
| 76 | + console.log(progress.uploaded, "/", progress.total); |
| 77 | + } |
| 78 | +}); |
| 79 | + |
| 80 | +await client.sendDocument(chatId, "./report.pdf", { progressId }); |
| 81 | +``` |
| 82 | + |
| 83 | +For sources whose size is not known in advance, `total` is `0` until the upload completes. |
0 commit comments