Safari 11.1: ajax/XHR form submission fails when input[type=file] is empty

后端 未结 6 552
南笙
南笙 2021-01-30 21:09

UPDATE: As of Webkit build r230963, this issue has been resolved in Webkit.

===========

Since the recent Safari 11.1 update on macOS and iOS, as

6条回答
  •  -上瘾入骨i
    2021-01-30 21:56

    This works for me to check if the input field is empty. If empty, disable the input field before created the FormData. After created the FormData, remove the "disabled" attribute. The difference to other answers is, that I search for "input[0].files.length == 0".

    // get the input field with type="file"
    var input = $('#myForm').find("input[type='file']")
    
    // add the "disabled" attribute to the input
    if (input[0].files.length == 0) {
      input.prop('disabled', true);
    }
    
    // create the formdata  
    var formData = new FormData($(this)[0]);
    
    // remove the "disabled" attribute
    input.prop('disabled', false);
    

提交回复
热议问题