【发布时间】:2021-06-25 17:36:10
【问题描述】:
我是 Python 新手,我使用 Flask 部署我的文件上传器应用程序,该文件上传器执行文本提取并返回图像及其模式以及图像。图片在 uploads 文件夹中正确上传,但是当我想显示它(返回功能)时,图片出现损坏,因为我收到 404 file not found 错误。
我的文件夹路径如下
.(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)
【问题讨论】:
标签: python python-3.x flask