簡體   English   中英

使用flask上傳xml文件和處理

[英]Uploading xml file and processing using flask

我正在開發一個 python 后端,我從前端向它發送一個 xml 文件。 這樣我就可以基於它生成python代碼並將內容顯示在前面。 我怎么能用燒瓶做到這一點? 由於我是燒瓶的初學者,這對我有幫助。 我附上了我在下面嘗試的代碼。 它對我不起作用。 我無法將 xml 文件保存到目錄中。

from flask import Flask,request,render_template
app=Flask(__name__,template_folder='templates')
from main import run
import os

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/submit/',methods=['POST'])
def upload():
    if request.method=='POST':
        uploaded_file = xmltodict.parse(request.get_data())
        file=os.path.join(app.config['upload'].uploaded_file.filename)
        uploaded_file.save(file)
    return "Successfully uploaded"
@app.route('/submit/')
def convert():
    path='upload'
    os.chdir(path)
    for file in os.listdir():
        if file.endswith(".py"):
            file_path = f"{path}\{file}"
            run(file_path,'tmp','python')
    return  "Code generated"

@app.route('/view/')
def view_python_script():
    # Folder path
    path='tmp'
    os.chdir(path)
    content=""
    for file in os.listdir():
        if file.endswith(".py"):
            file_path = f"{path}\{file}"
            with open(file_path, "r") as f:
                content =content+ f.read().replace('\n','<br>')
    return render_template('upload.html', details=content)

if __name__=="__main__":
    app.run(port=3000,debug=True)
    


    

我占用了這個: uploaded_file = request.files ['file_upload']

file_upload我從 html 傳遞它,參數name = "file_upload"包含在forminput 我遇到的問題是,當我想在另一個 html 頁面中共享它時,它會關閉並向我ValueError: I / O operation on closed file 不過好了,希望對你有幫助!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM