im designing an app that receives a XML file from a HTML page and send it to Python using Flask This is my HTML (frontend) code
<input type="file" id="files" class="form-control" accept=".xml" required />
<button onclick="send()" style="width: 150px;" class="btn btn-primary">Send</button>
And i am using Flask with Python to run my backend, i am using POSTMAN (i show it below) and i send a XML on it and return my what is supposed to return This is my Python code
@app.route('/Students', methods=['POST'])
def loaddata():
# this is a list
global dataArray
entry= request.data.decode('utf-8')
xmlentry= ET.fromstring(entry)
for student in xmlentry.findall('STUDENT'):
name= dte.find('NAME').text
address = dte.find('ADDRESS').text
new = DataObject(name,adress)
# DataObject its a class, i am using OOP
dataArray.append(new)
return jsonify({'Mensaje':'Students added to the database',})
but i dont know how to send the XML data from the HTML file and make it work with my Python Backend, i have used Fetch before but no for this particular case This is my JavaScript code where i have confusion
var file1 = document.getElementById("file");
function send() {
fetch('localhost:3000/Students', {
method: 'POST',
body: file1,
headers:{
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': '*',}})
.then(res => res.json())
.catch(err => {
console.error('Error:', err)
alert("Ocurrio un error, ver la consola")
})
.then(response =>{
console.log(response);
alert(response.Mensaje)
})
}
What i can do? This is my program tested with POSTMAN
file1
? Or do you relay on the User to enter a File into a Input element? – Twistyvar file1 = document.getElementById("file")
and in your HTML, I see<input type="file" id="files" class="form-control" accept=".xml" required />
, so this would not match up,file != files
– Twisty