为了账号安全,请及时绑定邮箱和手机立即绑定

Flask Upload Image 仅在 Localhost 上工作

Flask Upload Image 仅在 Localhost 上工作

慕桂英3389331 2022-08-25 14:58:29
大家好。我在使用烧瓶上传图像时遇到了一个小问题。我可以在localhost中的flask上上传图像(Web应用程序托管在我的桌面上)。但是当我在Online Server上加载烧瓶应用程序时,我总是遇到“FileNotFoundError”。我还将权限更改为777,但仍然无法正常工作。这是 html 上传代码。<form name="edit_vehicle_info" action="/vehicle_info_form/" method="POST" enctype="multipart/form-data" class="formfield">    <div class="form-group">    <label for="changeVehicleImg">Vehicle Image</label>    <input type="file" id="changeVehicleImg" name="changeVehicleImg" accept="image/*"></div>这是python上传代码。#vehicle_info_form@app.route('/vehicle_info_form/', methods=['GET', 'POST'])def vehicle_info_form():    try:        if request.method == "GET":            return render_template(                'vehicleInfo.html'            )        elif request.method == "POST":            inputVehicleImg = request.files['changeVehicleImg']            if inputVehicleImg.filename == "":                inputVehicleImg_filename = ""            else:                print(inputVehicleImg.filename)                print(app.config['UPLOAD_FOLDER_IMG_VEHICLE'])                inputVehicleImg_filename = secure_filename(inputVehicleImg.filename)                inputVehicleImg.save(os.path.join(app.config['UPLOAD_FOLDER_IMG_VEHICLE'], inputVehicleImg_filename))            print ('success')    except Exception as e:        print(e)        return redirect(url_for('vehicle_info_form'))python上传代码可以打印,然后之后,发生错误。inputVehicleImg.filenameapp.config['UPLOAD_FOLDER_IMG_VEHICLE']
查看完整描述

1 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

我通过更新.我用于获取完整的当前路径并添加应保存图像的路径。app.config['UPLOAD_FOLDER_IMG_VEHICLE']os.getcwd()


首先,我通过添加和来检查错误的根源是什么。tryexcept FileNotFoundError:


#vehicle_info_form

@app.route('/vehicle_info_form/', methods=['GET', 'POST'])

def vehicle_info_form():

    try:

        if request.method == "GET":

            return render_template(

                'vehicleInfo.html'

            )


        elif request.method == "POST":

            inputVehicleImg = request.files['changeVehicleImg']

            if inputVehicleImg.filename == "":

                inputVehicleImg_filename = ""

            else:

                print(inputVehicleImg.filename)

                print(app.config['UPLOAD_FOLDER_IMG_VEHICLE'])

                inputVehicleImg_filename = secure_filename(inputVehicleImg.filename)

                try:

                    inputVehicleImg.save(os.path.join(app.config['UPLOAD_FOLDER_IMG_VEHICLE'], inputVehicleImg_filename))

                except FileNotFoundError:

                    print("File does not exist")

            print ('success')

    except Exception as e:

        print(e)

        return redirect(url_for('vehicle_info_form'))

我尝试上传图像并在控制台中返回。File does not exist


然后,我检查图像应上传的路径。app.config['UPLOAD_FOLDER_IMG_VEHICLE']


print(app.config['UPLOAD_FOLDER_IMG_VEHICLE'])

在那里,我发现我的在线服务器中的路径不一样,所以我更新了它。


import os

path = os.getcwd()

print(path) 


UPLOAD_FOLDER_IMG_VEHICLE = path + "/apps/FMS/static/images/vehicleInfo"


app.config['UPLOAD_FOLDER_IMG_VEHICLE'] = UPLOAD_FOLDER_IMG_VEHICLE


查看完整回答
反对 回复 2022-08-25
  • 1 回答
  • 0 关注
  • 97 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信