A partial archive of https://discourse-mediawiki.wmflabs.org as of Saturday May 21, 2022.

File not completely uploaded on uploading in chunks

Mansi29ag

While writing the code for chunked uploading using API: Upload, my complete file does not get uploaded. It seems that only the first chunk gets uploaded.
Uploaded file: https://test.wikipedia.org/wiki/File:Jing2.png
Original file: jing2

But I cannot find any problem in my code as I am getting the right response code for all the uploads.
Response code

What could be the problem?

jeropbrenda

Hello:) I’ve gone through your code and the documentation at https://www.mediawiki.org/wiki/API:Upload#Chunked_uploading several times to try and understand why all the chunks are not being uploaded and this is what I think is the problem- You are not passing in the “offset” which will determine the starting point of the next chunk. So it’ll always be zero, which is the offset of the first chunk.
When you upload the first chunk, the response will include the starting point of the next chunk, so I passed this in the next chunk and it worked, i.e

#parameters for the first chunk
PARAMS_4 = {
    "action": "upload",
    "stash": 1,
    "filename": "chunk_test9.png",
    "filesize": file_size,
    "offset": 0,
    "format": "json",
    "token": CSRF_TOKEN,
    "ignorewarnings": 1
}
file = {'chunk':('{}.jpg'.format(index),chunk,'multipart/form-data')}
index += 1
R = S.post(URL, files=file, data=PARAMS_4)
DATA = R.json()
print(DATA)

#For the second and further chunks, pass in the filekey parameter as well
for chunk in chunks:
	PARAMS_4 = {
    "action": "upload",
    "stash": 1,
    "offset": DATA["upload"]["offset"],
    "filename": "chunk_test9.png",
    "filesize": file_size,
    "filekey": DATA["upload"]["filekey"],
    "format": "json",
    "token": CSRF_TOKEN,
	}
	file = {'chunk':('{}.jpg'.format(index),chunk,'multipart/form-data')}
	index += 1
	R = S.post(URL, files=file, data=PARAMS_4)
	DATA = R.json()
	print(DATA)
jeropbrenda

https://test.wikipedia.org/wiki/File:Chunk_test9.png is the file I tested.

Mansi29ag

Thanks @jeropbrenda. You are awesome.