API:Upload

There are three methods of uploading files via the API:


 * 1) Uploading a local file directly
 * 2) Uploading a local file in chunks (originally based on the Firefogg chunked upload protocol)
 * 3) Uploading a copy of a file elsewhere on the Web given by a URL

All of these methods require an account with the "upload" right.

Token
To upload files, an edit token is required. This token is the same for all pages, but changes at every login. The preferred method to obtain an edit token depends on the MediaWiki version: Currently, all older methods continue to work, but are deprecated.
 * Versions 1.24 and later: action=query&meta=tokens
 * Versions 1.20-1.23: action=tokens
 * Versions 1.19 and earlier: action=query&prop=info

Uploading directly
When uploading files directly, the request must use  as Content-Type or enctype,   will not work. The below is only a guide.

Chunked uploading
Since uploading a huge file in a single HTTP POST can be unreliable, the API also supports a chunked upload mode, where you make multiple requests with portions of the file. This is available in MediaWiki 1.20 and above, although prior to version 1.25, SVGs could not be uploaded via chunked uploading.

This is used by Extension:UploadWizard in browsers supporting FileAPI, uploading files in chunks of 1 megabyte, but you may choose your own size. This works in conjunction with the "stash" mode, to build a file up in pieces and then commit it at the end. action: upload stash: 1 format: json token: (token) offset: 0 filename: "filename.jpg" filesize: (total file size) chunk: (binary data of first chunk) You'll receive data like this: {   upload: { result: "Continue", filekey: "long scary filename.1.jpg", offset: (next chunk's starting point) imageinfo: {...} } } For the second and further chunks, pass in the filekey parameter as well: action: upload stash: 1 format: json token: (token) offset: (offset of start of this chunk) filename: "filename.jpg" filesize: (total file size) filekey: (filekey received from the previous Continue result) chunk: (binary data of next chunk) On the last chunk, you'll get back {   upload: { result: "Success", filekey: "long scary filename.1.jpg", offset: (next chunk's starting point) imageinfo: {...} (the data returned by this result is not completely accurate) } } You can then do a final upload using the file key to commit the upload out of the stash area: action: upload format: json token: (token) filename: "filename.jpg" filekey: (filekey received from the Success result) comment: upload comment text: file description

The result from the final upload will include the complete, accurate  object, comparable to what you would get from a non-chunked upload.

Sample Raw POST of a single chunk
This is an example of a multipart POST to, representing a single chunk. Note that you must unstash the file for it to appear in the Wiki once you have successfully uploaded all your chunks.

User-Agent:  Content-Type: multipart/form-data; boundary=--24b7c3bb-fb4d-45c3-937c-11c2e0c2525b Host: commons.wikimedia.org Cookie: Connection: Keep-Alive

--24b7c3bb-fb4d-45c3-937c-11c2e0c2525b Content-Disposition: form-data; name="filename" Content-Length: 20

UploadTest356456.png --24b7c3bb-fb4d-45c3-937c-11c2e0c2525b Content-Disposition: form-data; name="offset" Content-Length: 1

0 --24b7c3bb-fb4d-45c3-937c-11c2e0c2525b Content-Disposition: form-data; name="format" Content-Length: 4

json --24b7c3bb-fb4d-45c3-937c-11c2e0c2525b Content-Disposition: form-data; name="ignorewarnings" Content-Length: 1

1 --24b7c3bb-fb4d-45c3-937c-11c2e0c2525b Content-Disposition: form-data; name="filesize" Content-Length: 3

971 --24b7c3bb-fb4d-45c3-937c-11c2e0c2525b Content-Disposition: form-data; name="token" Content-Length: 42

 --24b7c3bb-fb4d-45c3-937c-11c2e0c2525b Content-Disposition: form-data; name="stash" Content-Length: 1

1 --24b7c3bb-fb4d-45c3-937c-11c2e0c2525b Content-Disposition: form-data; name="chunk"; filename="1.png" Content-Type: application/octet-stream Content-Length: 971

 --24b7c3bb-fb4d-45c3-937c-11c2e0c2525b--

Uploading from URL
This requires  in the wiki's local settings and an account with the   user right. By default this is done synchronously, so downloading large files may exceed PHP's max_execution_time and fail. Asynchronous mode will return a session key that can then be used to query the upload status (see below). Asynchronous mode requires  (  in later versions) in the wiki's local settings.

Asynchronous upload is no longer available as-of r81612.

Retrieving next upload status
For checking the status of an in-progress upload to the stash, or checking the progress of a file being published from the stash: