Sending a file with FormData fails in Safari (iOS and macOS).
In all browsers on iOS and Safari on macOS, when trying to submit the contents of a form with input[type=file] using FormData, the process fails if there is no file attached. This is because theProblems with SafariSo, it seems to be happening on iOS 11.3 or later, but blaming Safari doesn't solve the problem.
So, I created a code to work around this bug.
//Here is the normal code var data = new FormData($('#request-sheet-form').get(0)); //Here is the code we created If type=file and the content is empty, delete the element itself $("input[type=file]").each(function(i,j){ var name = $(j).attr("name"); if(!$('[name=' + name + ']').val()) { data.delete(name); } }); //Normal code from here on out $.ajax({ url: 'hoge.php', type: 'POST', data: data, processData: false, contentType: false, }).done(function(data, status, xhr){ });