API:Upload/zh

使用POST请求来上传一个文件.

有三种方法通过API上传文件：


 * 1) 直接上传本地文件
 * 2) 从URL复制上传一个文件
 * 3) 分块上传一个本地文件

所有这些方法需要一个拥有 权限的账号.

上傳警告
请注意，由于许多警告条件，上传可能会失败. 这可以通过设置  来覆盖. 以下列举了一些值得注意的警告：


 * 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.

例子
发出任何POST请求都是一个多步骤的过程：

 使用中描述的方法之一登录.  获取：

 發送包含CSRF令牌的POST請求以上傳文件.  

示例一：直接上传本地文件
When uploading files directly, the request must use  as Content-Type or enctype,   will not work.

示例二：從URL上傳文件
This requires  in the wiki's local settings and an account with the   user right.

示例三：分块上传文件
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 version 1.20 and above, although prior to version 1.25, SVGs could not be uploaded via chunked uploading. This is used by 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.

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.

参数历史

 * v1.21: 启用
 * v1.19: 启用
 * v1.18: 启用
 * v1.17: 启用, ,  ,
 * v1.18: 弃用
 * v1.17: 弃用

补充资料

 * 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:  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--

参见

 * - 允许用户登录
 * - 允许用户导入页面
 * - Upload limits according to user groups