首页 > 解决方案 > 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)

标签: pythonpython-3.xflask

解决方案


应该是位置错误。你能把错误日志也给我吗?将 FLASK_DEBUG=1。

到时候,试试这个,把你的上传文件夹保持在静态中,然后试试这个。

<img class="img-fluid" src="{{ url_for('static', filename='uploads/logo_full.png')}}" alt="Theme-Logo"/>

替换为您的文件名和属性。


推荐阅读