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 using 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, a token is required. This token is identical to the edit token and is the same regardless of target filename, but changes at every login. Unlike other tokens, it cannot be obtained directly, so one must obtain and use an edit token instead.

Uploading

 * Parameters


 * filename - Target filename
 * comment - Upload comment. Also used as the initial page text for new files if text parameter not provided
 * text - Initial page text for new files.
 * token - Edit token
 * watch - Watch the page
 * ignorewarnings - Ignore any warnings
 * file - File contents
 * url - Url to fetch the file from
 * sessionkey</tt> - Session key returned by a previous upload that failed due to warnings, or (with httpstatus) The upload_session_key of an asynchronous upload

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

Uploading from URL
This requires $wgAllowCopyUploads = true</tt> in the wiki's local settings and an account with the upload_by_url</tt> 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 $wgEnableAsyncDownload = true</tt> in the wiki's local settings.

Retrieving upload status
For asynchronous uploads using upload-by-url the initial response will include a session key; pass this with httpstatus</tt> parameter to fetch upload status. This session key does not remain valid across login sessions.

Sample Raw Upload
Newlines are \r\n. User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1) Content-Type: multipart/form-data; boundary=---8ce5ac3ab79ab2c Host: commons.wikimedia.org Cookie: Content-Length: Connection: Keep-Alive

-8ce5ac3ab79ab2c Content-Disposition: form-data; name="action" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit

upload -8ce5ac3ab79ab2c Content-Disposition: form-data; name="filename" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit

Apple.gif -8ce5ac3ab79ab2c Content-Disposition: form-data; name="text" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit

text -8ce5ac3ab79ab2c Content-Disposition: form-data; name="comment" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit

comment -8ce5ac3ab79ab2c Content-Disposition: form-data; name="token" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit

0ff12678952302357892acc565412393+\ -8ce5ac3ab79ab2c Content-Disposition: form-data; name="format" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit

xml -8ce5ac3ab79ab2c Content-Disposition: form-data; name="file"; filename="Apple.gif" Content-Type: application/octet-stream; charset=UTF-8 Content-Transfer-Encoding: binary

<Gif in binay> -8ce5ac3ab79ab2c--