Dia di sana, saya telah mencoba mencari solusi untuk masalah saya selama 2 hari sekarang tetapi masih belum menemukan apa pun. Mungkin hanya merindukannya. Aplikasi web flask saya dijalankan di Microsoft Azure. Kesalahannya sangat sederhana, tidak ditemukan tetapi saya tidak tahu bagaimana menemukan jalan yang benar.

Saya mendapatkan kesalahan berikut:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 2069, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 2054, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 2051, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1501, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1499, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1485, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/ivar/PycharmProjects/Course8.1/app.py", line 78, in genpanel
    f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
  File "/usr/local/lib/python3.8/dist-packages/werkzeug/datastructures.py", line 3013, in save
    dst = open(dst, "wb")
FileNotFoundError: [Errno 2] No such file or directory: '/uploads/GenPanelOverzicht_DG-3.1.0_HAN.xlsx'

Saya memiliki kode labu berikut:

import os

from flask import Flask, render_template, request
from werkzeug.utils import secure_filename

UPLOAD_FOLDER = '/uploads'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
FLASK_DEBUG=1
@app.route('/')
def home():
    """ home page """
    return render_template("basis.html")
@app.route('/genpanel', methods=['GET', 'POST'])
def genpanel():
    """ genpanel upload page """
    print(os.getcwd())
    try:
        if request.method == 'POST':
            f = request.files['file']
            filename = secure_filename(f.filename)
            f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            md5sum, dictionary = Genpanelanalyser.fileopener(
                '/uploads/'+f.filename)
            md5sumold = Genpanelanalyser.getmd5sum()
            if md5sum != md5sumold:
                Genpanelanalyser.database(md5sum, dictionary)
                message = "File upload was successfull"
                os.remove('/uploads/' + f.filename)
                return render_template("genpanel.html", message=message)
            else:
                message = "Data allready exists"
                os.remove('/uploads/'+f.filename)
                return render_template("genpanel.html", message=message)
        else:
            message = "Upload a file"
            return render_template("genpanel.html", message=message)
    except FileExistsError or FileNotFoundError:
        message = "File not found"
        return render_template("genpanel.html", message=message)


if __name__ == '__main__':
    app.run(debug=True)

Ini kode html saya:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="Danielle Dunnewold, Ivar van den Akker en Moshtach
    Ismail" >
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Manual</title>
    <link rel="stylesheet" href="static/Style.css">
</head>
<body>
    <!-- horizontaal menu -->
    <div class="scrollmenu">
        <a href="/">HOME</a>
        <a href="/results">APPLICATION</a>
        <a href="/manual">MANUAL</a>
        <a href="/download">DOWNLOAD RESULTS</a>
        <a href="/genpanel">UPLOAD GENEPANEL</a>
    </div>
<H2>Upload a new genepanel</H2>
    <form action = '/genpanel' method = "POST" enctype = "multipart/form-data">
         <input type = "file" name = "file" />
         <input type = "submit" value = "Submit" />
    </form>
    <br>
    <p>{{ message }}</p>
</body>
</html>

Jika seseorang mengetahui solusi atau halaman di mana saya dapat menemukan solusi atau cara untuk menemukan solusi, jangan ragu untuk berkomentar dan membantu saya. Jika ada informasi lebih lanjut yang diperlukan saya dapat memberikan.

0
Ivar2001 24 Mei 2021, 17:57

2 jawaban

Jawaban Terbaik

Folder unggah didefinisikan sebagai app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER, tetapi sepertinya ada, jadi baris kode f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) gagal. Anda dapat mencoba

  1. buat folder "upload", dan itu akan berada di level yang sama dengan template.
  2. os.remove akan terlihat seperti di bawah ini (hapus dulu /)
os.remove('uploads/' + f.filename)
0
simpleApp 24 Mei 2021, 18:56

Saya membuat fungsi untuk mendapatkan jalur ke folder unggahan dan meneruskannya ke fungsi saya sehingga jalurnya akan selalu benar. Saya mencoba membuat folder tetapi itu tidak berhasil. Anda di mana benar tentang /.

Fungsi untuk mendapatkan jalur:

import  os
def getpath():
    patht = os.getcwd()
    path = patht+"/uploads/"
    return path

Fungsi baru di app.py:

UPLOAD_FOLDER = uploads.getpath.getpath()
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


@app.route('/')
def home():
    """ home page """
    return render_template("basis.html")
@app.route('/genpanel', methods=['GET', 'POST'])
def genpanel():
    """ genpanel upload page """
    try:
        if request.method == 'POST':
            f = request.files['file']
            filename = secure_filename(f.filename)
            f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            md5sum, dictionary = Genpanelanalyser.fileopener(
                UPLOAD_FOLDER+f.filename)
            md5sumold = Genpanelanalyser.getmd5sum()
            if md5sum != md5sumold:
                Genpanelanalyser.database(md5sum, dictionary)
                message = "File upload was successfull"
                os.remove(UPLOAD_FOLDER+f.filename)
                return render_template("genpanel.html", message=message)
            else:
                message = "Data allready exists"
                os.remove(UPLOAD_FOLDER+f.filename)
                return render_template("genpanel.html", message=message)
        else:
            message = "Upload a file"
            return render_template("genpanel.html", message=message)
    except FileExistsError or FileNotFoundError:
        message = "File not found"
        return render_template("genpanel.html", message=message)


if __name__ == '__main__':
    app.run()
0
Ivar2001 25 Mei 2021, 10:48