I have read tons of Q&A about AJAX file upload but none of them works for me. I'm using Chrome 59 and Safari 10.
What is wrong with this AJAX call?
$(function() {
$('#filesubmit').submit(function(e) {
e.preventDefault();
let file = $('#inputfile').prop('files')[0];
let formData = new FormData();
formData.append('file', file);
$.ajax({
type: 'POST',
url: 'getArchFromNet/',
data: formData,
processData: false,
contentType: false,
cache: false,
success: function(response) {
alert('ok');
}
});
});
});
Maybe the problem is in my HTML, which is a bit messy, but I don't think so... anyway, here it is:
<form action="POST" id="filesubmit" style="display:none;">
<p>
<input id="inputfile" name="inputfile" type="file" onchange="$('#filesubmit').submit();" />
<input type="submit" name="sendfile" value="sendfile" />
</p>
</form>
<p id="net_actions">
<a id="load_net" href="javascript:$('#inputfile').click();"><img src="{% static 'upload.png' %}" alt="upload a network" /></a>
<a id="save_net" href="saveNet/?job_id=" type="application/octet-stream"><img src="{% static 'download.png' %}" alt="download the network" /></a>
</p>
Specifically, what happens in my HTML is this: when a user clicks on the #load_net
anchor, the browser clicks the #inputfile
file input; when the user has chosen a file, the browser clicks the #sendfile
submit input and this triggers my JS function.
The problem is that an empty payload is being sent, instead of my file. I have also tried to use let file = $('#inputfile').files[0];
and let file = $('#inputfile').get(0).files[0];
but none of them works.