I am having trouble writing a program for sending html form using an ajax call in jQuery. Here is my upload.html and form.js file:
$(document).ready(function() {
$('form').on('submit', function(event) {
$.ajax({
type : 'POST',
url : '/uploadhc',
data : $('#hc')
})
.done(function(data) {
if (data.error) {
$('#errorAlert').text(data.error).show();
$('#successAlert').hide();
}
else {
$('#successAlert').text(data.file).show();
$('#errorAlert').hide();
}
});
event.preventDefault();
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<meta charset="UTF-8">
<title>File Upload</title>
<script type=text/javascript src="{{ url_for('static', filename='form.js') }}"></script>
</head>
<body>
<h1>Please upload all the corresponding files</h1>
<div id="upload">
<form id="upload-hc" >
<input type="file" name="file" accept=".csv" id="hc">
<input type="submit" value="go">
</form>
<br>
<p id="successAlert" style="display:none;">Success!</p>
<p id="errorAlert" style="display:none;">Fail!</p>
</div>
<script>
</script>
</body>
</html>
Here is my Flask server:
import os
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
@app.route("/")
def index():
return render_template("upload.html")
@app.route("/uploadhc", methods=['POST'])
def uploadhc():
target = os.path.join(APP_ROOT, "DataSource/")
if not os.path.isdir(target):
os.mkdir(target)
print request.files
if 'file' not in request.files:
error = "Missing data source!"
return jsonify({'error': error})
file = request.files['file']
fileName = "DCData.csv"
destination = '/'.join([target, fileName])
file.save(destination)
success = "Success!"
return jsonify({'file': success})
if __name__ == "__main__":
app.run(port=4555, debug=True)
When I try to select a csv file and submit the HTML form, the server says the request.files is ImmutableMultiDict([]), which is empty. Any idea how to send the file to my server? Thanks!