M
M
MrEluzium2021-06-02 21:04:06
Python
MrEluzium, 2021-06-02 21:04:06

Why is empty file uploaded to python vk_api conversation?

I'm trying to upload a local file to a conversation. I tried both through the built-in vk_api methods , and the method from the documentation. Here is the code for the latter:

method = vk.docs.getMessagesUploadServer(peer_id=event.message.peer_id)["upload_url"]
file = requests.post(method, files={'file': open(f'data/{filename}', "rb")})
if file.status_code == 200:
    file = file.json()['file']
    uploaded = vk.docs.save(file=file)
    vk.messages.send(
        key=(bot_config['key']),
        server=(bot_config['server']),
        ts=(bot_config['ts']),
        random_id=get_random_id(),
        message=f' {first_name} {last_name}\n\n{text}',
        chat_id=event.chat_id,
        attachment=f"doc{uploaded['doc']['owner_id']}_{uploaded['doc']['id']}")
else:
    answer(' Не получилось загрузить файл. Попробуйте обновить данные.', event.chat_id)


The bot doesn't throw any errors, docs.save returns file data and a link, but the file itself is empty. It is attached, the size in the message is correct, but there is no pikchi itself.

60b7c746b7f40973073188.jpeg
60b7c74c27854087265554.jpeg

I'm using vk_api 11.9.4 (latest); Longpoll 5.131 (latest)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
ZinovNikita, 2022-03-22
@MrEluzium

It helped me to replace the method with docs.getWallUploadServer and the file was uploaded to the group files, but at the same time it became possible to download it in the conversation (I have Node js)

S
soremix, 2021-06-02
@SoreMix

The code is working. Maybe the file is broken? Does the document open on other devices? From the phone, let
’s either try to download the file and see what loads

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question