TestXiong 2022-12-23 15:25 采纳率: 100%
浏览 5
已结题

关于#python#的问题:python分段上传

我设置的明明是1M,为什么每次上传给我上传了2M。



import math
import os

import requests


def upload_slice_file(url, file_path):
    chunk_size = 1048576  # 1M分段
    # filename = file_path.split("\\") # [0][0:-4] 分割去.zip
    total_size = os.path.getsize(file_path)  # 计算总字节流
    current_chunk = 1
    total_chunk = math.ceil(total_size / chunk_size)
    # while current_chunk <= total_chunk:
    for i in range(total_chunk):
        start = (current_chunk - 1) * chunk_size
        print('sta',start)
        end = min(total_size, start + chunk_size)
        print('end',end)
        with open(file_path, 'rb') as f:
            f.seek(start)
            file_chunk_data = f.read(end - start)
            print(len(file_chunk_data))
            token = 'C8xX4aT1wG1x'
            pid = 3021
            veakey = 'G9tZ8jZ8i'
            datafd = 'filename={}&token={}&pid={}&veakey={}&total_blob_num={}&blob_num={}'.format(veakey, token, pid,
                                                                                                  veakey, total_chunk,
                                                                                                  current_chunk)

        data = {"file": (file_chunk_data, 'application/octet-stream')}

        res = requests.post(url + datafd, data=data)
        print(res.json(), res.url)
        # with requests.post(url+datafd,data=data) as response:
        #     assert response.status_code == 200

        current_chunk = current_chunk + 1


upload_slice_file('http://182.168.1.170:8080/ggfile/upzipnew.php?', 'M8qX3wZ7t.zip')

img

  • 写回答

2条回答 默认 最新

  • TestXiong 2022-12-23 15:43
    关注

    困惑已久的我还是找到问题了。
    不应该用,data = {"filename": (file_chunk_data, 'application/octet-stream')}来传。
    直接data = file_chunk_data就可以了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月31日
  • 已采纳回答 12月23日
  • 创建了问题 12月23日