0

I'm trying to get the data to write the data contained in the file to the DB. But I'm having problems with loading the csv on the page and then transmitting it and starting reading in the relative function.The page in question is formed as follows:

<div class = "content">
    <div class = "container">
        <div class = "main">
            <h1> <strong> LOADING RESULTS </strong> </h1> <br/>
            <div class = "round">
            
                <form name = "controlEsits" action = "controlEsits" method = "get" enctype = "multipart / form-data">
                <fieldset>
                    <legend> UPLOAD RESULTS FROM CSV </legend> <br/>
                    <strong> Academic Year </strong> <input type = "text" id = "aa" name = "Academic Year" value = "2020"> <br/>
                    <strong> Session </strong> <input type = "text" id = "session" name = "Session" value = "Winter"> <br/>
                    <strong> Test Type </strong> <input type = "text" id = "Test Type" name = "Test Type" value = "English"> <br/>
                    <strong> Results File </strong> <input type = "file" id = "file" name = "FileN" accept = "results / csv"> <br/> <br/>
                    
                    <input class = "btn" type = "submit" value = "Upload Results from File" id = "btncheck" formaction = "/ insertionDBEsiti"> <br/> <br/>
                
                    <font color = "red"> <b> {{warning}} </b> </font>
                  </div>
                </form>
            </div>
        </div>
    </div>
{% include 'footer.html'%}

but i believe the problem may be in the part of code that gets the file:

@app.route('/inserimentoDBEsiti', methods=['POST', 'GET'])
def inserimentoDBEsiti():
    aa = request.form.get('aa')
    session = request.form.get('Session')
    typeP = request.form.get('Test Type')
    file = request.files["fileN"]
    app.config["result"] = "../temp/"
    file.save(os.path.join(app.config["result"], file.filename))
    fileLocation = (os.path.join(app.config["result"], file.filename))

what am I doing wrong?

giofar
  • 19
  • 5
  • What error are you seeing here? – v25 Jan 09 '21 at 14:02
  • Hi, this is the error: "werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'file' " – giofar Jan 09 '21 at 14:06

1 Answers1

1

For your form tag, generate the action attribute with url_for which needs to match the view function which handles the post request. Also you can't have a spaces in multipart/form-data, and the request method should be POST:

<form name="controlEsits" action="{{url_for('inserimentoDBEsiti')}}" method="POST" enctype="multipart/form-data">

Take the formaction attribute out of the submit button:

<input class="btn" type="submit" value="Upload Results from File" id="btncheck">

Also in the flask app, the key of request.files needs to match the name attribute of the file input cell, so where you have:

<input type="file" id="file" name="FileN" accept="results/csv">

It's then read with:

file = request.files["FileN"]

Notice the case sensitivity, you had request.files["fileN']

I'd be careful with all the extra spaces in your HTML particularly within attributes.

v25
  • 7,096
  • 2
  • 20
  • 36
  • @giofar no probs. After that you'll just need to sort out the the file saving code. I'd avoid paths that traverse to a higher directory (`../temp`) and try to keep stuff within subfolders of your app. – v25 Jan 09 '21 at 14:59