【问题标题】:Error 404 (GET) request error on Flask appFlask 应用程序上的错误 404 (GET) 请求错误
【发布时间】: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


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 感谢您的回复。您提供的代码需要添加到 HTML 布局中。然而,这种情况并非如此。用户上传了一个随机图像,该图像存储在 UPLOAD_INPUT_IMAGES_FOLDER = '/static/uploads/' 虽然图像已正确存储在上述路径 '/static/uploads/' 中,但当我尝试将其显示回来时,它似乎已损坏( def upload_image():)
    • 流程如下: 1. 用户上传图片。该应用程序检查扩展,图像是否处理成功,然后使用 ocr 2 提取文本。OCR 效果很好,图像存储在上述路径中。 3. 现在用户将相同的图像显示回前端,这会导致 404 错误
    • 知道了。您可以使用创建应用程序的文件(app.py 或类似文件)编辑问题吗?
    • 我已经用路径和文件结构更新了代码:)
    猜你喜欢
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 2017-10-16
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多