API:Upload/ja

ファイルをアップロードする POST リクエストです.

There are three methods of uploading files via the API:


 * 1) Uploading a local file directly
 * 1) Uploading a copy of a file from a URL
 * 1) Uploading a local file in chunks

All of these methods require an account with the  right.



Upload warnings
Note that an upload may fail due to a number of warning conditions. This can be overridden by setting. Some notable warnings include:


 * exists: A file with the given name already exists. If this warning is ignored, the uploaded file will replace the existing file.
 * no-change: A file with the given name already exists and is exactly the same as the uploaded file.
 * duplicate-version: A file with the given name already exists and and old version of that file is exactly the same as the uploaded file.
 * was-deleted: a file with the given name used to exist but has been deleted.
 * duplicate: The uploaded file exists under a different (or the same) name. Uploading a duplicate may be undesirable.
 * duplicate-archive: The uploaded used to exist under a different (or the same) name but has been deleted. This may indicate that the file is inappropriate and should not be uploaded.
 * badfilename: The file name supplied is not acceptable on this wiki, for instance because it contains forbidden characters

例
Making any POST request is a multi-step process:

  Log in, via one of the methods described in.  を取得します:

  Send a POST request, with the CSRF token to upload a file.  

Example 1: Upload a local file directly
When uploading files directly, the request must use  as Content-Type or enctype,   will not work.



レスポンス


Example 2: Upload file from URL
This requires  in the wiki's local settings and an account with the   user right.



レスポンス


サンプル コード


例 3: 分割アップロード
重いファイルを1回の HTTP POST でアップロードしようとすると確実性が不安定になるため、API は分割アップロードといって、当該ファイルを部分に分けて数回に分けてアップロードを要求する方法をサポートします. MediaWiki バージョン 1.20 以降に提供を開始したものの、バージョン1.25以前は、 SVG形式のファイルは分割アップロードの対象外でした. 1 メガバイト単位でファイルを分割してアップロードするにはFileAPIをサポートするブラウザので使用されるものの、分割するサイズの単位は利用者が決定します. This works in conjunction with the stash mode, to build a file up in pieces and then commit it at the end.

Step 1: Pass content for the first chunk


For all the chunks except the last chunk you will receive this:

Step 2: Pass filekey parameter for second and further chunks
You will obtain the  parameter from the previous continue result as well:



For the last chunk, you will receive this:

Step 3: Final upload using the filekey to commit the upload out of the stash area


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



サンプル コード


起こりうるエラー
See also the section on #Upload warnings above.

<span id="Parameter_history">

パラメーターの履歴

 * v1.21:    を導入しました
 * v1.19:      を導入しました
 * v1.18:    を導入しました
 * v1.17:,  ,  ,   を導入しました
 * v1.18:  を廃止予定にしました
 * v1.17:  を廃止予定にしました

<span id="Additional_notes">

追加的な注記

 * To check the status of an in-progress upload to the stash or the progress of a file being published from the stash, send a POST request:

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

User-Agent: <YOUR 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

<YOUR_CSRF_TOKEN_HERE> --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

<RAW_BYTES_FROM_FILE_HERE> --24b7c3bb-fb4d-45c3-937c-11c2e0c2525b--

<span id="See_also">

関連項目

 * - 利用者がログインできるようにします
 * - 利用者がページを取り込めるようにします
 * - Upload limits according to user groups