Reputation: 85
Here is my HTML:
{% extends "layout.html" %}
{% block title %}
Picture
{% endblock %}
{% block main %}
<form action="/picture" method="post">
<div>
<p>Profile picture (32mb max).</p>
<input type="file" id="picture" name="picture" enctype="multipart/form-data">
<input type="submit" value="Upload">
</div>
</form>
{% endblock %}
Here is the .py :
import sqlite3
import traceback
import sys
import os
from flask import Blueprint, render_template, redirect, session, request, flash
from application import profileName, uploadPicture, profilePicture, allowed_file
from werkzeug.utils import secure_filename
# Set Blueprints
picture = Blueprint('picture', __name__,)
@picture.route("/picture", methods=["GET", "POST"])
def pictureFunction():
if request.method == "POST":
# check if the post request has the file part
if "picture" not in request.files:
flash("No file part")
return redirect("/picture")
file = request.files["picture"].read()
# if user does not select file, browser also submit a empty part without filename
if file.filename == "":
flash("No selected file")
return redirect("/picture")
# Check if all conditions are satisfied
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join("/static", filename))
return print(filename)
When I try to upload a .jpg (allowed file), I always end up in having: "No file part" of the first "if".
I don't understand why. Can somebody help me figure out why I can't upload this file?
Upvotes: 3
Views: 1807
Reputation: 7621
The enctype
attribute should be part of the <form>
tag, not your file <input>
:
So instead do something like:
<form action="/picture" method="post" enctype="multipart/form-data">
<div>
<p>Profile picture (32mb max).</p>
<input type="file" id="picture" name="picture">
<input type="submit" value="Upload">
</div>
</form>
Upvotes: 5