zoukankan      html  css  js  c++  java
  • requests上传文件

    """
    requests上传文件时,如果文件名是中文,会导致上传失败,参考:https://www.cnblogs.com/liaofeifight/p/5807901.html

    """

    # 不修改源码解决方式如下:
    from django.conf import settings
    import requests
    import urllib
    from urllib import parse
    import json
    import logging
    import os
    from io import BytesIO
    
    
     params = {
                "t": 4
            }
            data_bin = BytesIO(fileobj.read())
    
            # encode_data = encode_multipart_formdata(fileobj.read())
            # data = encode_data[0]
            ext = os.path.splitext(fileobj.name.rstrip())[1].lower().split('.')[-1]
            # files = {"file": (fileobj.name, fileobj.read(), 'image/%s' % ext)}
            # ("filename", "fileobject", "content-type", "headers")
            name = parse.quote(filename.encode('utf-8'))
            files = {"file":  (name, data_bin, 'image/%s' % ext)}
            
            try:
                # wb_data = requests.post(url, data=json.dumps(params), headers=self.headers)
                wb_data = requests.post(url, data=params, files=files)
                data_bin.close()
                # print(wb_data.content)
                wb_data = json.loads(wb_data.content.decode('utf-8'))
    
                if wb_data['code'] == 0:
                    # print(wb_data['path'])
  • 相关阅读:
    开发细节
    html
    java学习
    Promise对象
    强制转换和隐式转换
    借助防抖解决输入框的非空校验
    setTimeout
    Symbol类型
    js API
    vue 使用mixin
  • 原文地址:https://www.cnblogs.com/lajiao/p/9340851.html
Copyright © 2011-2022 走看看