<p>When working with the API, it is sometimes necessary to send a relatively large file to the server. For example, when sending a message with a photo/video attachment or when setting the current user's profile picture.</p>
<p>The file's binary content is then split into parts. All parts must have the same size ( <strong>part_size</strong> ) and the following conditions must be met:</p>
<li><code>part_size % 1024 = 0</code> (divisible by 1KB)</li>
<li><code>524288 % part_size = 0</code> (512KB must be evenly divisible by <strong>part_size</strong>)</li>
</ul>
<p>The last part does not have to satisfy these conditions, provided its size is less than <strong>part_size</strong>.</p>
<p>Each part should have a sequence number, <strong>file_part</strong>, with a value ranging from 0 to 2,999.</p>
<p>After the file has been partitioned you need to choose a method for saving it on the server. Use <ahref="/method/upload.saveBigFilePart">upload.saveBigFilePart</a> in case the full size of the file is more than <strong>10 MB</strong> and <ahref="/method/upload.saveFilePart">upload.saveFilePart</a> for smaller files.</p>
<p>Each call saves a portion of the data in a temporary location on the server to be used later. The storage life of each portion of data is between several minutes and several hours (depending on how busy the storage area is). After this time, the file part will become unavailable. To increase the time efficiency of a file save operation, we recommend using a call queue, so X pieces of the file are being saved at any given moment in time. Each successful operation to save a part invokes the method call to save the next part. The value of X can be tuned to achieve maximum performance.</p>
<p>When using one of the methods mentioned above to save file parts, one of the following <ahref="/api/errors#400-bad-request">data input errors</a> may be returned:</p>
<ul>
<li>FILE_PARTS_INVALID - Invalid number of parts. The value is not between <code>1..3000</code></li>
<li>FILE_PART_INVALID: The file part number is invalid. The value is not between <code>0 and 2,999</code>.</li>
<li>FILE_PART_TOO_BIG: The size limit (512 KB) for the content of the file part has been exceeded</li>
<li>FILE_PART_EMPTY: The file part sent is empty</li>
<li>FILE_PART_SIZE_INVALID - 512KB cannot be evenly divided by <strong>part_size</strong></li>
<li>FILE_PART_SIZE_CHANGED - The part size is different from the size of one of the previous parts in the same file</li>
</ul>
<p>While the parts are being uploaded, an <ahref="https://en.wikipedia.org/wiki/MD5">MD5 hash</a> of the file contents can also be computed to be used later as the <strong>md5_checksum</strong> parameter in the <ahref="/constructor/inputFile">inputFile</a> constructor (since it is checked only by the server, for encrypted secret chat files it must be generated from the encrypted file).
After the entire file is successfully saved, the final method may be called and passed the generated <ahref="/type/InputFile">inputFile</a> object. In case the <ahref="/method/upload.saveBigFilePart">upload.saveBigFilePart</a> method is used, the <ahref="/constructor/inputFileBig">inputFileBig</a> constructor must be passed, in other cases use <ahref="/constructor/inputFile">inputFile</a>.</p>
<ul>
<li><ahref="/method/messages.sendMedia">messages.sendMedia</a> - Sends a media file to a chat</li>
<li><ahref="/method/messages.uploadMedia">messages.uploadMedia</a> - Uploads a media file to a chat, without sending it, returning only a <ahref="/type/MessageMedia">MessageMedia</a> constructor that can be used to later send the file to multiple chats, without reuploading it every time. </li>
<li><ahref="/method/photos.uploadProfilePhoto">photos.uploadProfilePhoto</a> - Used to set a <ahref="#uploading-profile-or-chat-pictures">profile or chat picture or video</a></li>
</ul>
<p>The file save operation may return one of the following <ahref="/api/errors#400-bad-request">data input errors</a>:</p>
<ul>
<li>FILE_PARTS_INVALID: The number of file parts is invalid The value is not between 1 and 3,000.</li>
<li>FILE_PART_Х_MISSING: Part X (where X is a number) of the file is missing from storage. Try repeating the method call to resave the part.</li>
<p>Telegram allows grouping photos into <ahref="https://telegram.org/blog/albums-saved-messages">albums</a> and generic files (audio, docuemnts) into media groups. </p>
<p>To do this, <ahref="/method/messages.sendMultiMedia">messages.sendMultiMedia</a> is used, wrapping each <ahref="/type/InputMedia">InputMedia</a> constructor (uploaded or pre-existing, maximum 10 per media group) into an <ahref="/constructor/inputSingleMedia">inputSingleMedia</a> constructor, optionally providing a custom per-file caption in <code>message</code>. </p>
<p>For photo albums, clients should display an album caption only if exactly one photo in the group has a caption, otherwise no album caption should be displayed, and only when viewing in detail a specific photo of the group the caption should be shown.<br>
Other grouped media can display a caption under each file. </p>
<p>For some types of documents like GIFs, <ahref="/method/messages.getDocumentByHash">messages.getDocumentByHash</a> can be used to search for the document on Telegram servers.
The SHA256 hash of the file is computed, and it is passed along with the file's mime type and size to the method: if the file type is correct and the file is found, a <ahref="/constructor/document">document</a> is returned.</p>
<h3><aclass="anchor"href="#uploading-profile-or-chat-pictures"id="uploading-profile-or-chat-pictures"name="uploading-profile-or-chat-pictures"><iclass="anchor-icon"></i></a>Uploading profile or chat pictures</h3>
<p>User profile pictures can be uploaded using the <ahref="/method/photos.updateProfilePhoto">photos.uploadProfilePhoto</a> method: the actual profile picture has to be <ahref="#uploading-files">uploaded as for normal files</a>.<br>
<ahref="/method/photos.updateProfilePhoto">photos.uploadProfilePhoto</a> can also be used to reupload previously uploaded profile pictures. </p>
<p>Animated profile pictures are also supported, by populating the <code>video</code> constructor: square MPEG4 videos up to <code>1080x1080</code> are supported, <code>800x800</code> is the recommended resolution.<br>
The <code>video_start_ts</code> is a floating point UNIX timestamp in seconds, indicating the frame of the video that should be used as static preview. </p>
<p><ahref="/api/channel">Chat, channel and supergroup</a> profile photos and videos can be uploaded using <ahref="/method/messages.editChatPhoto">messages.editChatPhoto</a> (<ahref="/api/channel#basic-groups">basic groups</a>) or <ahref="/method/channels.editPhoto">channels.editPhoto</a> (<ahref="/api/channel">channels, supergroups</a>).<br>
<p>There are methods available to download files which have been successfully uploaded. The schema of the types and methods used is presented below:</p>
<p>For photos, <ahref="/constructor/inputPhotoFileLocation">inputPhotoFileLocation</a> is used:</p>
<ul>
<li><code>id</code>, <code>file_reference</code> and <code>access_hash</code> taken from the <ahref="/constructor/photo">photo</a> constructor</li>
<li><code>thumb_size</code> taken from the <code>type</code> field of the desired <ahref="/type/PhotoSize">PhotoSize</a> of the <ahref="/constructor/photo">photo</a></li>
</ul>
</li>
<li>
<p>For profile pictures of users, channels, supergroups and groups, <ahref="/constructor/inputPeerPhotoFileLocation">inputPeerPhotoFileLocation</a> has to be used:</p>
<ul>
<li><code>peer</code> is the identifier of the peer whose photo we want to download</li>
<li><code>big</code> is used to choose whether to download the full-resolution picture, or just the thumbnail</li>
<li><code>photo_id</code> is extracted from the <ahref="/constructor/chatPhoto">chatPhoto</a> or <ahref="/constructor/userProfilePhoto">userProfilePhoto</a> of the desired profile photo</li>
</ul>
</li>
<li>
<p>For documents, <ahref="/constructor/inputDocumentFileLocation">inputDocumentFileLocation</a> is used:</p>
<ul>
<li><code>id</code>, <code>file_reference</code> and <code>access_hash</code> taken from the <ahref="/constructor/document">document</a> constructor</li>
<li>If downloading the thumbnail of a document, <code>thumb_size</code> should be taken from the <code>type</code> field of the desired <ahref="/type/PhotoSize">PhotoSize</a> of the <ahref="/constructor/photo">photo</a>; otherwise, provide an empty string.</li>
</ul>
</li>
<li>
<p>For previews of sticker sets, <ahref="/constructor/inputStickerSetThumb">inputStickerSetThumb</a> is used (note: to download stickers and previews of stickers use the document method described above for documents):</p>
<ul>
<li><code>stickerset</code> is set to the <ahref="/type/InputStickerSet">InputStickerSet</a> constructor generated from <ahref="/constructor/stickerSet">stickerSet</a></li>
<li><code>thumb_version</code> is copied from the same field in <ahref="/constructor/stickerSet">stickerSet</a></li>
</ul>
</li>
<li>
<p>For encrypted secret chat and telegram passport documents, respectively <ahref="/constructor/inputEncryptedFileLocation">inputEncryptedFileLocation</a> and <ahref="/constructor/inputSecureFileLocation">inputSecureFileLocation</a> have to be used, with parameters extracted from <ahref="/constructor/encryptedFile">encryptedFile</a> and <ahref="/constructor/secureFile">secureFile</a> (<ahref="/passport">passport docs</a>).</p>
</li>
<li>
<p>For livestream chunks, <ahref="/constructor/inputGroupCallStream">inputGroupCallStream</a> is used:</p>
<ul>
<li><code>call</code> contains the related group call ID+access hash, taken from the <ahref="/constructor/groupCall">groupCall</a> constructor.</li>
<li><code>time_ms</code> specifies the timestamp to fetch</li>
<li><code>scale</code> specifies the duration of the video segment to fetch in milliseconds, by bitshifting <code>1000</code> to the right <code>scale</code> times: <code>duration_ms := 1000 >> scale</code></li>
<li><code>video_channel</code> specifies the video channel to fetch</li>
<li><code>video_quality</code> specifies the selected video quality (0 = lowest, 1 = medium, 2 = best)</li>
<p>For old <strong>deprecated</strong> photos, if the client has cached some old fileLocations with the <strong>deprecated</strong><code>secret</code> identifier, <ahref="/constructor/inputFileLocation">inputFileLocation</a> or <ahref="/constructor/inputPhotoLegacyFileLocation">inputPhotoLegacyFileLocation</a> is used (this is mainly used for backwards compatibility with bot API file IDs, all user clients must use the modern <ahref="/constructor/inputPhotoFileLocation">inputPhotoFileLocation</a> file IDs): </p>
<li>All fields are taken from the previously cached fileLocation except for <code>file_reference</code>, <code>access_hash</code> and <code>id</code>, which are taken from the <ahref="/constructor/photo">photo</a> constructor (the last two fields are used only if available, in which case <ahref="/constructor/inputPhotoLegacyFileLocation">inputPhotoLegacyFileLocation</a> is used instead of <ahref="/constructor/inputFileLocation">inputFileLocation</a>).</li>
</ul>
</li>
</ul>
<p>The size of each file in bytes is available, which makes it possible to download the file in parts using the parameters <strong>offset</strong> and <strong>limit</strong>, similar to the way files are uploaded.</p>
<p>If <strong>precise</strong> flag is not specified, then </p>
<ul>
<li>The parameter <strong>offset</strong> must be divisible by 4 KB.</li>
<li>The parameter <strong>limit</strong> must be divisible by 4 KB.</li>
<li>1048576 (1 MB) must be divisible by <strong>limit</strong>.</li>
</ul>
<p>If <strong>precise</strong> is specified, then</p>
<ul>
<li>The parameter <strong>offset</strong> must be divisible by 1 KB.</li>
<li>The parameter <strong>limit</strong> must be divisible by 1 KB.</li>
<li><strong>limit</strong> must not exceed 1048576 (1 MB).</li>
</ul>
<p>In any case the requested part should be within one 1 MB chunk from the beginning of the file, i. e.</p>
<p>The file download operation may return a <code>FILE_REFERENCE_EXPIRED</code> error (or another error starting with <code>FILE_REFERENCE_</code>): in this case, the <code>file_reference</code> field of the input location must be <ahref="/api/file_reference">refreshed</a>.
The file download operation may return an <ahref="/constructor/upload.fileCdnRedirect">upload.fileCdnRedirect</a> constructor: in this case, <ahref="/cdn">these</a> instructions must be followed for downloading CDN files.
The file download operation may also return one of the following <ahref="/api/errors#400-bad-request">data input errors</a>:</p>
<ul>
<li>FILE_ID_INVALID: The file address is invalid</li>
<li>OFFSET_INVALID: The <strong>offset</strong> value is invalid</li>
<li>LIMIT_INVALID: The <strong>limit</strong> value is invalid</li>
<li>FILE_MIGRATE_X: The file is in the datacenter No. X</li>
<p>In order to confirm the integrity of the downloaded file, clients are recommended to verify hashes for each downloaded part, as for <ahref="/cdn">CDN DCs</a>.
<ahref="/method/upload.getFileHashes">upload.getFileHashes</a> contain <ahref="/type/FileHash">FileHash</a> constructors. Each of these constructors contains the SHA-256 hash of a part of the file that starts with <code>offset</code> and takes <code>limit</code> bytes.</p>
<p>Before saving each portion of the data received from the DC into the file, the client can confirm that its hash matches the hash that was received from the master DC. If missing a hash for any file part, client developers must use the <ahref="/method/upload.getFileHashes">upload.getFileHashes</a> method to obtain the missing hash.</p>
<h4><aclass="anchor"href="#handling-audio-video-and-vector-previews"id="handling-audio-video-and-vector-previews"name="handling-audio-video-and-vector-previews"><iclass="anchor-icon"></i></a>Handling audio, video and vector previews</h4>
<p>A <ahref="/constructor/photoStrippedSize">photoStrippedSize</a> (with type <code>i</code>) is an extremely low-res thumbnail, embedded directly inside media location objects.<br>
It should be shown to the user in chat message previews, or while still downloading the most appropriately sized <ahref="/constructor/photoSize">photoSize</a> through the media DCs as <ahref="#downloading-files">described above</a>. </p>
<p>The stripped <code>bytes</code> payload should be inflated to a JPG payload as seen <ahref="https://github.com/telegramdesktop/tdesktop/blob/1757dd856b84d23f83d4e562c94dde825f6eb40c/Telegram/SourceFiles/ui/image/image.cpp#L43">here »</a>.</p>
<p>Messages with <ahref="/animated_stickers">animated</a>, <ahref="/stickers#video-stickers">video</a>, <ahref="/stickers">normal</a> stickers can have a compressed svg (< 300 bytes) to show the outline of the sticker before fetching the actual sticker.
Sticker outlines will have a <code>j</code> type <ahref="/constructor/photoPathSize">photoPathSize</a> thumbnail. </p>
<p>This specific vector thumbnail consists of an <ahref="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths">SVG path</a>, specially encoded to save space.<br>
<p><code>path</code> will contain the actual SVG path that can be directly inserted in the <code>d</code> attribute of an <ahref="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path">svg <path> element</a>: </p>
<p>A <ahref="/constructor/videoSize">videoSize</a> constructor is typically used for <ahref="#animated-profile-pictures">animated profile pictures</a> and video previews.</p>
<p>Remote HTTP files sent by inline bots <ahref="/type/BotInlineResult">in response to inline queries</a> and in other places are represented by <ahref="/type/WebDocument">WebDocument</a> constructors.
When forwarding such remote HTTP files, they should be sent using <ahref="/type/InputMedia">external InputMedia constructors</a>.
Remote HTTP files can only be downloaded directly by the client if contained in a <ahref="/constructor/webDocumentNoProxy">webDocumentNoProxy</a> constructor: in this case, the file is deemed safe to download (this is the case for HTTPS files from certain trusted domains).</p>
<p>However, if the remote file is contained in a <ahref="/constructor/webDocument">webDocument</a>, to avoid leaking sensitive information the file must be downloaded through telegram's servers.
This can be done in a manner similar to <ahref="#downloading-files">normal files</a>, with the difference that <ahref="/method/upload.getWebFile">upload.getWebFile</a> must be used, instead.</p>
<li><ahref="/constructor/inputWebFileLocation">inputWebFileLocation</a> is simply generated by taking the <code>url</code> and <code>access_hash</code> fields of the <ahref="/constructor/webDocument">webDocument</a> constructor.</li>
<li><ahref="/constructor/inputWebFileGeoPointLocation">inputWebFileGeoPointLocation</a> is used to download a server-generated image with the map preview from a <ahref="/constructor/geoPoint">geoPoint</a>.<ul>
<li><code>geo_point</code> is generated from the <code>lat</code>, <code>long</code><code>accuracy_radius</code> parameters of the <ahref="/constructor/geoPoint">geoPoint</a></li>
<li><code>access_hash</code> is the access hash of the <ahref="/constructor/geoPoint">geoPoint</a></li>
<li><code>w</code> - Map width in pixels before applying scale; 16-1024</li>
<li><code>h</code> - Map height in pixels before applying scale; 16-1024</li>
<p>It is recommended that large queries (<ahref="/method/upload.getFile">upload.getFile</a>, <ahref="/method/upload.saveFilePart">upload.saveFilePart</a>, <ahref="/method/upload.getWebFile">upload.getWebFile</a>) be handled through a separate session and a separate connection, in which no methods other than these should be executed. If this is done, then data transfer will cause less interference with <ahref="/api/updates">getting updates</a> and other method calls.</p>