mongotop mongotop - 6 months ago 231
Python Question

Getting an empty ImmutableMultiDict object from jQuery request data

Trying to upload a file to server but the ImmutableMultiDict object is returning empty.

upload.html

<html>
<head>
<title>Upload File Ajax</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#upload-file-btn').click(function() {
var form_data = new FormData($('#upload-file')[0]);
form_data.append('file', $('input[type=file]')[0].files[0]);
console.log(form_data)
$.ajax({
url: '/uploadajax',
data: form_data,
type: 'POST',
contentType: false,
cache: false,
processData: false,
async: false,
success: function(data) {
console.log('Success!');
// console.log(data);
},
});
});
});

</script>

</head>
<body>
<form action="" name="upload-file" id="upload-file" method="post" enctype="multipart/form-data">
<fieldset>
<label for="file">Select a file</label>
<input name="file" id="file" type="file">
</fieldset>
<fieldset>
<button id="upload-file-btn" type="button">Upload</button>
</fieldset>
</form>

</body>
</html>


Flask

app = Flask(__name__)

@app.route('/')
def index():
return render_template("upload.html")


@app.route('/uploadajax', methods = ['POST'])
def upldfile():
logging.debug('request.method : %s', request.method)
logging.debug('request.files : %s', request.files)
logging.debug('request.args : %s', request.args)
logging.debug('request.form : %s', request.form)
logging.debug('request.values : %s', request.values)
logging.debug('request.headers : %s', request.headers
logging.debug('request.data : %s', request.data)

upload_files = request.files.getlist("file")

logging.debug('upload_files : %s', upload_files)

file_val = request.files

for k, v in file_val.items() :logging.debug('key : %s -- value : %s ', k, v)


# if request.method == 'POST':file_val = request.files['file']
return render_template('upload.html')


if __name__ == '__main__':
app.run(host='127.0.0.1', debug=True, port=2345, use_reloader=True)


This is the log data

016-05-03 00:05:30,500 - root - DEBUG - request.method : POST
2016-05-03 00:05:30,500 - root - DEBUG - request.files : ImmutableMultiDict([])
2016-05-03 00:05:30,500 - root - DEBUG - request.args : ImmutableMultiDict([])
2016-05-03 00:05:30,501 - root - DEBUG - request.form : ImmutableMultiDict([])
2016-05-03 00:05:30,501 - root - DEBUG - request.values : CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([])])
2016-05-03 00:05:30,501 - root - DEBUG - request.headers : Referer: http://127.0.0.1:2345/
Origin: http://127.0.0.1:2345
Content-Length: 368
User-Agent: Mozilla/5.0....
Connection: keep-alive
X-Requested-With: XMLHttpRequest
Host: 127.0.0.1:2345
Accept: */*
Accept-Language: en-US,en;q=0.8
Content-Type: false
Accept-Encoding: gzip, deflate2016-05-03 03:13:58,236 - root - DEBUG - request.data : ------WebKitFormBoundaryA8jmfBTRKGJFtMS
Content-Disposition: form-data; name="file"; filename="test_upload.txt"
Content-Type: text/plain

qbcdefgh
abcdefgh
------WebKitFormBoundaryA8jmfBTRKGJFtMS
Content-Disposition: form-data; name="file"; filename="test_upload.txt"
Content-Type: text/plain

qbcdefgh
abcdefgh
------WebKitFormBoundaryA8jmfBTRKGJFtMS--


2016-05-03 00:05:30,501 - root - DEBUG - upload_files : []

Answer

It's your jQuery version too old to cause this.

When I use 2.1.1, the code runs Okay.

BTW, you do NOT need form_data.append.