Sending a file with FormData fails in Safari (iOS and macOS).
In all browsers on iOS and Safari on macOS, when attempting to submit form content containing input[type=file] using FormData, the process fails if no file is attached. This is a Safari bug and seems to occur in iOS 11.3 and later, but blaming Safari does not solve the problem.
Therefore, we have 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 created code If type=file, delete the element itself if the content is empty
$("input[type=file]").each(function(i,j){
var name = $(j).attr("name");
if(!$('[name=' + name + ']').val()) {
data.delete(name);
}
});
// Normal code from here on
$.ajax({
url: 'hoge.php',
type: 'POST',
data: data,
processData: false,
contentType: false,
}).done(function(data, status, xhr){
});