Python Flask filestorage 上传文件服务器


    file_list = request.files.getlist('fileList')
    if not file_list:
        return error('', 'Please upload file')
    file = file_list[0]
    r = requests.get(File_url['get_guid'])
    guid = r.text
    file_name = file.filename
    f = (file_name, file.read(), file.content_type)
    request_data = {
        'file': f,
        'fileGuid': (None, guid),
        'isVersionControl': (None, 'N'),
        'isPrivate': (None, False)
    }
    ret = requests.post(File_url['upload_url'], files=request_data, headers={'accept': 'application/json'})
    if ret.status_code != 200:
        return error('', 'upload file error')

    return success(guid, '')

做一个文件上传的需求,被困住了

1、flask 的文件是flaskStorage,而我们文件服务器所接受的是file
2、用multipart/form-data上传并不会被识别到。

问题1:
一篇文章的启发,发现POST files 中可以接收 file-tuple
file-tuple can be a 2-tuple ('filename', fileobj), 3-tuple ('filename', fileobj, 'content_type')
or a 4-tuple ('filename', fileobj, 'content_type', custom_headers)
问题2:
Header里传’accept’: ‘application/json’ 还没搞懂为什么这样 参考的是 https://stackoverflow.com/questions/73818803/how-to-pass-multipart-form-data-into-python-post-request

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值