Showing
1 changed file
with
50 additions
and
42 deletions
| @@ -21,17 +21,57 @@ def upload_form(): | @@ -21,17 +21,57 @@ def upload_form(): | ||
| 21 | """显示文件上传表单""" | 21 | """显示文件上传表单""" |
| 22 | return render_template('main.html') | 22 | return render_template('main.html') |
| 23 | 23 | ||
| 24 | -@app.route('/status/<filename>') | ||
| 25 | -def check_status(filename): | ||
| 26 | - """检查文件处理状态,并返回状态和统计信息""" | ||
| 27 | - status_info = processing_status.get(filename, {'status': 'processing', 'stats': None}) | ||
| 28 | - return json.dumps(status_info) | 24 | +@app.route('/upload', methods=['POST']) |
| 25 | +def upload_file(): | ||
| 26 | + """处理文件上传和启动异步处理""" | ||
| 27 | + if 'file' not in request.files: | ||
| 28 | + flash('没有文件部分', 'error') | ||
| 29 | + return redirect(url_for('upload_form')) | ||
| 30 | + | ||
| 31 | + file = request.files['file'] | ||
| 32 | + | ||
| 33 | + if file.filename == '': | ||
| 34 | + flash('未选择文件', 'error') | ||
| 35 | + return redirect(url_for('upload_form')) | ||
| 36 | + | ||
| 37 | + if file and allowed_file(file.filename): | ||
| 38 | + filename = secure_filename(file.filename) | ||
| 39 | + filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) | ||
| 40 | + filepath = os.path.abspath(filepath) # 转换为绝对路径 | ||
| 41 | + | ||
| 42 | + try: | ||
| 43 | + file.save(filepath) | ||
| 44 | + print(f'文件已保存到 {filepath}') | ||
| 45 | + | ||
| 46 | + # 初始化处理状态 | ||
| 47 | + processing_status[filename] = {'status': 'processing', 'stats': None} | ||
| 48 | + | ||
| 49 | + # 启动后台线程处理文件 | ||
| 50 | + thread = threading.Thread(target=handle_file_processing, args=(filepath, filename)) | ||
| 51 | + thread.start() | ||
| 52 | + | ||
| 53 | + # 重定向到等待页面,并传递文件名以跟踪状态 | ||
| 54 | + return redirect(url_for('waiting_page', filename=filename)) | ||
| 55 | + except Exception as e: | ||
| 56 | + flash(f'文件上传失败: {str(e)}', 'error') | ||
| 57 | + return redirect(url_for('upload_failure')) | ||
| 58 | + else: | ||
| 59 | + flash('文件类型不允许', 'error') | ||
| 60 | + return redirect(url_for('upload_form')) | ||
| 61 | + | ||
| 29 | 62 | ||
| 30 | @app.route('/waiting/<filename>') | 63 | @app.route('/waiting/<filename>') |
| 31 | def waiting_page(filename): | 64 | def waiting_page(filename): |
| 32 | """显示等待页面,并传递文件名""" | 65 | """显示等待页面,并传递文件名""" |
| 33 | return render_template('waiting.html', filename=filename) | 66 | return render_template('waiting.html', filename=filename) |
| 34 | 67 | ||
| 68 | + | ||
| 69 | +@app.route('/status/<filename>') | ||
| 70 | +def check_status(filename): | ||
| 71 | + """检查文件处理状态,并返回状态和统计信息""" | ||
| 72 | + status_info = processing_status.get(filename, {'status': 'processing', 'stats': None}) | ||
| 73 | + return json.dumps(status_info) | ||
| 74 | + | ||
| 35 | @app.route('/upload-success') | 75 | @app.route('/upload-success') |
| 36 | def upload_success(): | 76 | def upload_success(): |
| 37 | """文件处理成功页面""" | 77 | """文件处理成功页面""" |
| @@ -97,40 +137,8 @@ def handle_file_processing(filepath, filename): | @@ -97,40 +137,8 @@ def handle_file_processing(filepath, filename): | ||
| 97 | 'stats': None | 137 | 'stats': None |
| 98 | } | 138 | } |
| 99 | 139 | ||
| 100 | -@app.route('/upload', methods=['POST']) | ||
| 101 | -def upload_file(): | ||
| 102 | - """处理文件上传和启动异步处理""" | ||
| 103 | - if 'file' not in request.files: | ||
| 104 | - flash('没有文件部分', 'error') | ||
| 105 | - return redirect(url_for('upload_form')) | ||
| 106 | - | ||
| 107 | - file = request.files['file'] | ||
| 108 | - | ||
| 109 | - if file.filename == '': | ||
| 110 | - flash('未选择文件', 'error') | ||
| 111 | - return redirect(url_for('upload_form')) | ||
| 112 | - | ||
| 113 | - if file and allowed_file(file.filename): | ||
| 114 | - filename = secure_filename(file.filename) | ||
| 115 | - filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) | ||
| 116 | - filepath = os.path.abspath(filepath) # 转换为绝对路径 | ||
| 117 | - | ||
| 118 | - try: | ||
| 119 | - file.save(filepath) | ||
| 120 | - print(f'文件已保存到 {filepath}') | ||
| 121 | - | ||
| 122 | - # 初始化处理状态 | ||
| 123 | - processing_status[filename] = {'status': 'processing', 'stats': None} | ||
| 124 | - | ||
| 125 | - # 启动后台线程处理文件 | ||
| 126 | - thread = threading.Thread(target=handle_file_processing, args=(filepath, filename)) | ||
| 127 | - thread.start() | ||
| 128 | - | ||
| 129 | - # 重定向到等待页面,并传递文件名以跟踪状态 | ||
| 130 | - return redirect(url_for('waiting_page', filename=filename)) | ||
| 131 | - except Exception as e: | ||
| 132 | - flash(f'文件上传失败: {str(e)}', 'error') | ||
| 133 | - return redirect(url_for('upload_failure')) | ||
| 134 | - else: | ||
| 135 | - flash('文件类型不允许', 'error') | ||
| 136 | - return redirect(url_for('upload_form')) | ||
| 140 | +if __name__ == '__main__': | ||
| 141 | + # 如果上传文件夹不存在,则创建 | ||
| 142 | + if not os.path.exists(app.config['UPLOAD_FOLDER']): | ||
| 143 | + os.makedirs(app.config['UPLOAD_FOLDER']) | ||
| 144 | + app.run(debug=True) |
-
Please register or login to post a comment