Fixed Safari/iOS file upload over AJAX

I think we need to reopen this. Seems that iOS and Safari have huge issues with file upload elements in AJAX forms.
 
Heres some more information I found while debugging:

All the requests are made on the Edit Resource page. Tested on Safari.

1. Successful: This the request when I select the option "Delete the current icon".
Code:
------WebKitFormBoundarym7hycS8DUACXOUUR
Content-Disposition: form-data; name="icon_action"

delete
------WebKitFormBoundarym7hycS8DUACXOUUR

2. Successful: This the request when I select a file to be uploaded.
Code:
------WebKitFormBoundarylwwBDJOpGBS6kJcJ
Content-Disposition: form-data; name="upload"; filename="icon.jpg"
Content-Type: image/jpeg

------WebKitFormBoundarym7hycS8DUACXOUUR

3. Failed: This the request when I select no icon to be uploaded. The upload field isn't even touched here.
Code:
------WebKitFormBoundaryf6ZV5aNBRkALlEfA
Content-Disposition: form-data; name="upload"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryf6ZV5aNBRkALlEfA

Feel free to ping me if I can help.
 
It's an issue with Safari. I am not sure if it's a bug, but the solution is to remove the empty file fields from the form before uploading the content.

As you can see from the request form data. There is no empty upload field in the successful requests.
Something like this should work for you.


Code:
$('input[type=file]',form).each(function() {
        var files = $(this).prop('files');
        if ( files != undefined && files.length <= 0 ) {
            formData.delete($(this).attr('name'));              
        }
    });

Source: https://stackoverflow.com/questions...n-safari-ios-and-os-x-broke-after-last-update
 
Back
Top Bottom