API:Upload/ja

API でファイルをアップロードする方法には、以下の 3 種類があります:


 * 1) ローカル ファイルを直接アップロードする
 * 2) Firefogg 分割アップロード プロトコルを使用して、ローカル ファイルを分割アップロードする
 * 3) 指定した URL にあるファイルの複製をアップロードする

これらの方法はどれも、「アップロード」権限があるアカウントが必要です.

トークン
ファイルをアップロードするには、編集トークンが必要です. トークンはアップロード先ファイル名によらず同一ですが、ログインするたびに変化します. その他のトークンとは異なり、トークンを直接取得できないため、代わりに編集トークンを取得して使用してください. 編集トークンは action=tokens で取得できます (MW 1.20 以降). 以下の方法でも取得できます:

アップロード

 * パラメーター


 * 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 - 編集トークン
 * watch - ページをウォッチする
 * ignorewarnings - あらゆる警告を無視する
 * 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

直接のアップロード
When uploading files directly, the request must use  as Content-Type or enctype,   will not work. The below is only a guide.

分割アップロード
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.

This is used by Extension:UploadWizard/ja 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 first request) 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: {...} } }

You can then do a final upload using the file key to commit the upload out of the stash area.

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> ($wgAllowAsyncCopyUploads = true</tt> in later versions) in the wiki's local settings.

Asynchronous upload is no-longer available as-of r81612.

アップロードの状態の取得
For checking the status of an in-progress upload to the stash, or checking the progress of a file being published from the stash:

生のアップロードのサンプル
改行文字は \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

アップロード -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

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

コメント -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> -8ce5ac3ab79ab2c--