亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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上傳代碼。#[email protected]('/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 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號