python - Flask 应用程序上的错误 404 (GET) 请求错误
问题描述
我是 Python 新手,使用 Flask 部署我的文件上传器应用程序,该文件上传器执行文本提取并返回图像及其模式和图像。图像在上传文件夹中正确上传,但是当我想显示它(返回功能)时,图像出现损坏,因为我收到 404 文件未找到错误。
我的文件夹路径如下
.(main)
├── app
│ ├── static (folder)
│ └── templates (folder)
│ └── uploads //save all the uploaded images in this (folder)
| └── upload.py (file) <-- this file contains the code below
| └── init.py (file)
| └── views.py (file)
|
├── _tests
│ ├── footer.html
│ └── header.html
根据相对路径,它是 /app/static/uploads/
但我收到错误“GET /app/uploads/34.png HTTP/1.1”404 -
我尝试使用 ..app/uploads/ & ../../uploads/ 但它仍然无法正常工作,我可以进行哪些更改来解决此问题?
此外,以下带有 upload_image 的函数太长。如何减少或拆分功能?
**代码:upload.py **
UPLOAD_INPUT_IMAGES_FOLDER = '/static/uploads/'
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_IMAGE_EXTENSIONS
@app.route('/upload', methods=['POST'])
def upload_image():
if request.method == 'POST':
# checks whether or not the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(os.getcwd() +
UPLOAD_INPUT_IMAGES_FOLDER, filename))
flash('File successfully uploaded')
# calls the ocr_processing function to perform text extraction
# extract the text and display it
return render_template('upload.html',
msg='Processed successfully!',
img_src=UPLOAD_INPUT_IMAGES_FOLDER + file.filename)
else:
flash('Allowed image types are abc, abc, abc')
return redirect(request.url)
解决方案
应该是位置错误。你能把错误日志也给我吗?将 FLASK_DEBUG=1。
到时候,试试这个,把你的上传文件夹保持在静态中,然后试试这个。
<img class="img-fluid" src="{{ url_for('static', filename='uploads/logo_full.png')}}" alt="Theme-Logo"/>
替换为您的文件名和属性。
推荐阅读
- html - 如何在页脚中定义变量
- node.js - 如何使用 sinon 从节点流中存根错误?
- java - TCP 通过 TETRA 协议
- docker - 从一个容器到另一个容器执行代码(即从 API 容器在工作容器上执行脚本)
- python - 将 RowNumber 添加到字典列表
- ios - 如何在 Swift 中重置 UICollectionView
- java - 在Java中以同一方法迭代多个列表的最佳方法是什么
- javascript - D3 设计可重用组件以在“调用”期间接受参数
- spring-boot - 如何为所有扩展类型请求制作控制器?
- excel - 如何锁定宏文件中记录的单元格并使它们具有不同的颜色(也锁定)