【问题标题】:Send file using POST from a Python使用 POST 从 Python 发送文件
【发布时间】:2015-09-22 00:32:36
【问题描述】:

我有网址,我需要发送视频文件。 出于这个原因,我写了这段代码:

import requests

upload_url = 'https://cs506200.vk.me/upload_video_new.php?act=add_video&mid=21844505&oid=21844505&vid=171170813&fid=0&tag=93bb46ee&hash=e238f469a32fe7eee85a&swfupload=1&api=1'
file_ = {'file': ('video.mp4', open('video.mp4', 'rb'))}
r = requests.post(upload_url, files=file_)

print (r.text)

我收到一个错误: {“错误”:“无效文件”}

但在这种情况下:

<!DOCTYPE HTML>
<html>
 <head>
  <meta charset="utf-8">
 </head>
 <body>  
<form enctype="multipart/form-data" action="https://cs506200.vk.me/upload_video_new.php?act=add_video&mid=21844505&oid=21844505&vid=171170813&fid=0&tag=93bb46ee&hash=e238f469a32fe7eee85a&swfupload=1&api=1" method="POST" target="_blank">

<input type="file" name="video_file" />

<input type="submit" value="submit" name="submit" />
</form>
 </body>
</html>

一切正常。 我做错了什么?

【问题讨论】:

    标签: python post file-upload python-requests vk


    【解决方案1】:

    您使用了错误的字段名称:

    file_ = {'file': ('video.mp4', open('video.mp4', 'rb'))}
    

    将字段命名为 file,而您的表单使用 video_file

    <input type="file" name="video_file" />
    

    使用正确的字段名称很重要,请更正您的参数:

    file_ = {'video_file': ('video.mp4', open('video.mp4', 'rb'))}
    

    【讨论】:

    • 你是对的!这两天我解决不了这个问题。谢谢!
    猜你喜欢
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 2021-08-07
    • 2016-02-04
    相关资源
    最近更新 更多