我无法删除图片,但可以使用Flask在相同目录下正常工作

时间:2018-09-29 14:24:31

标签: flask flask-restful flask-mysql

我正在尝试从文件夹中删除图像,但无法正常工作,并出现错误:FileNotFoundError:[WinError 2]系统找不到指定的文件:“ static \ images \ 2018194259_a476v_engelhart-tilburg_inside-track_text_font_product.jpg” >

但是插入在相同的上传目录下工作得很好

UPLOAD_FOLDER = 'static\images'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

插入代码:

def insert():
cursor = db.cursor()

if request.method == "POST":
    flash("Data Inserted Successfully")
    name = request.form['name']
    email = request.form['email']
    phone = request.form['phone']

    image = request.files['imgfile'] #myfile is name of input tag

    if image and allowed_file(image.filename):
        fileTemp = secure_filename(image.filename)
        time_p = time.strftime('%Y%H%M%S')

        filename = time_p+"_"+fileTemp

        image.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))

        path = filename
    empty=''
    if path is empty:
        return "You have not uploading a image"
    else:
        cursor.execute("INSERT INTO student_flask (name, email, phone)VALUES (%s, %s, %s)", (name, path, phone))
        db.commit()
        cursor.close()

    return redirect(url_for('Index'))

删除图像代码:

@app.route('/delete/<string:id_data>', methods = ['GET'])
def delete(id_data):

   imgname = image_name(id_data)
   mna = imgname[0]
   os.remove(os.path.join(app.config['UPLOAD_FOLDER'], mna))
   return redirect(url_for('Index'))

1 个答案:

答案 0 :(得分:0)

查询数据库,首先获取图像名称。如果它本身是一个对象。 Image.name或对象属性。 Foo.image。在删除功能中,必须运行commit才能使更改生效。另外,id应该是int

@app.route('/delete/<int:id>', methods=['GET', 'POST'])
def delete(id=None):