猿问

Flask Upload Image 仅在 Localhost 上工作

大家好。我在使用烧瓶上传图像时遇到了一个小问题。我可以在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']


慕桂英3389331
浏览 98回答 1
1回答

慕标琳琳

我通过更新.我用于获取完整的当前路径并添加应保存图像的路径。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():&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; if request.method == "GET":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return render_template(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'vehicleInfo.html'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; elif request.method == "POST":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputVehicleImg = request.files['changeVehicleImg']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if inputVehicleImg.filename == "":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputVehicleImg_filename = ""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(inputVehicleImg.filename)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(app.config['UPLOAD_FOLDER_IMG_VEHICLE'])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputVehicleImg_filename = secure_filename(inputVehicleImg.filename)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputVehicleImg.save(os.path.join(app.config['UPLOAD_FOLDER_IMG_VEHICLE'], inputVehicleImg_filename))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except FileNotFoundError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("File does not exist")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ('success')&nbsp; &nbsp; except Exception as e:&nbsp; &nbsp; &nbsp; &nbsp; print(e)&nbsp; &nbsp; &nbsp; &nbsp; 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 ospath = os.getcwd()print(path)&nbsp;UPLOAD_FOLDER_IMG_VEHICLE = path + "/apps/FMS/static/images/vehicleInfo"app.config['UPLOAD_FOLDER_IMG_VEHICLE'] = UPLOAD_FOLDER_IMG_VEHICLE
随时随地看视频慕课网APP

相关分类

Python
我要回答