Python请求,无法将图像上传到multipart / form-data

时间:2017-09-12 13:54:55

标签: python file-upload python-requests

我正在使用python请求将jpeg图像文件上传到多部分表单。

如果我通过浏览器正常上传图像来检查请求,我就会看到这一点:

var alphaOrder = function(sortDescending){
var $container = $('section.grid'),
    $containerInner = $container.find('> ul'),
    alphaOrder = $container.find('li').sort(function(a, b) {
        // return String.prototype.localeCompare.call($(a).data('name').toLowerCase(), $(b).data('name').toLowerCase());
            var an = a.getAttribute('data-name'),
                bn = b.getAttribute('data-name'),
                pos = sortDescending ? -1 : 1,
                neg = sortDescending ? 1 : -1;

            if(an > bn) { return pos; }
            if(an < bn) { return neg; }
            return 0;
    });
    // console.log(typeof alphaOrder);
$containerInner.detach().empty().append(alphaOrder);
$container.append($containerInner);

这是我用来上传表单中第一张图片的python代码(最多可以上传8张图片),但首先我应该能够上传至少第一张图片:

------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="uploadphotos"

true
------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="deletephoto"

0
------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_0"; filename="1 KIN 
GRD.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path0"

C:\fakepath\1 KIN GRD.jpg
------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="e$phMain$UploadButton0"

Upload photo
------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_1"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path1"


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_2"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path2"


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_3"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path3"


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_4"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path4"


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_5"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path5"


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_6"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path6"


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_7"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path7"


------WebKitFormBoundaryKGkGxMtaz943aFXQ--

但我无法上传图片。

编辑:这不是同一个问题。我的问题与这个具体案例有关:在这种特定情况下,正确的请求是什么?我是否必须发送包含所有字段的多部分请求(因为我会发布所有图像)?

EDIT2:我找到了解决方案:

files = {'upload_input_0': ("test_image.jpg", open('test_image.jpg', 'rb'),'image/jpg')}
r = requests.post(upload_image_url, headers = headers, files=files)

这是一个完美的解决方案!

0 个答案:

没有答案