Python实现大文件分片上传记录

Python大文件分片上传记录

后端

flask 示例

  • config.py
import pathlib
from datetime import datetime

# file
base_dir = pathlib.Path.cwd()

SLICE_DATA = f'{base_dir}/upload/slice_data/{datetime.now().strftime("%Y-%m-%d")}'
COMBINATION_DATA = f'{base_dir}/upload/combination_data/{datetime.now().strftime("%Y-%m-%d")}'
from flask import Flask, render_template, request
from datetime import datetime
import os
import pathlib as pd
from config import *

app = Flask(__name__)

# app.config['SEND_FILE_MAX_AGE_DEFAULT'] = timedelta(seconds=1)
app.jinja_env.auto_reload = True
@app.route('/')
def hello():
    return render_template('index.html')



def combination_fun(folder_path, source_name):

    com_path = pd.Path(COMBINATION_DATA)
    if not pd.Path.exists(com_path):
        os.makedirs(com_path)

    ready_folder = os.listdir(folder_path)
    ready_sort_folder = sorted(ready_folder, key=lambda x: int(x.split('_')[-1]))
    with open(f'{COMBINATION_DATA}/{source_name}', 'wb') as write_f:
        for item in ready_sort_folder:
            slice_item = os.path.join(folder_path, item)
            print(slice_item)
            with open(slice_item, "rb") as read_f:
                content = read_f.read()
                write_f.write(content)





@app.route('/upload_file', methods=['POST'])
def upload_file():
    file = request.files['file']
    name_index = request.form['name_index']
    complete = request.form['complete']
    process = request.form['process']

    source_name = request.form['filename']
    filename = ''.join(source_name.split('.')[0:-1])
    file_path = fr'{SLICE_DATA}/{filename}'
    p_file_path = pd.Path(file_path)
    if not pd.Path.exists(p_file_path):
        os.makedirs(p_file_path)
    file.save(fr'{file_path}/{name_index}')

    print(f'process:{round(float(process)*100, 2)}%')
    if complete == 'true':
        print('传输完成')
        print('准备合成')
        combination_fun(file_path, source_name)


    return 'ok'


if __name__ == "__main__":
    app.run(debug=True)

前端示例



    
    upload
    


    
    



    


你可能感兴趣的:(Python实现大文件分片上传记录)