当前位置: 首页>后端>正文

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)

前端示例
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>upload</title>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
    <input type="file" name="file" id="file">
    <button id="upload" onClick="upload()">upload</button>
<!--    <div class="rate_div" >-->
<!--        上传进度:<span id="rate_nums">0</span>-->
<!--    </div>-->
    <script type="text/javascript">
        var bytesPerPiece = 1024 * 1024; // 每个文件切片大小定为1MB .
        var totalPieces;
        //发送请求
        function upload() {
            var blob = document.getElementById("file").files[0];
            var start = 0;
            var end;
            var index = 1;
            var filesize = blob.size;
            var filename = blob.name;
            var complete = false;

            //计算文件切片总数
            totalPieces = Math.ceil(filesize / bytesPerPiece);
            while(start < filesize) {
                end = start + bytesPerPiece;
                if(end > filesize) {
                    end = filesize;
                    complete= true
                }
<!--                var nums_node = document.getElementById("rate_nums");-->


                var chunk = blob.slice(start,end);//切割文件    
                var sliceIndex= blob.name + '_' +index;
                var formData = new FormData();
                formData.append("file", chunk);
                formData.append("name_index", sliceIndex);
                formData.append("filename", filename);
                formData.append('complete',complete);
                formData.append('process', index / totalPieces);

                console.log(formData);
                $.ajax({
                    url: '/upload_file',
                    type: 'POST',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                }).done(function(res){ 

                }).fail(function(res) {

                });
                start = end;
                index++;
            }

        }
    </script>
</body>
</html>

https://www.xamrdz.com/backend/3dv1923627.html

相关文章: