oracle / cordova-plugin-wkwebview-file-xhr

Cordova Plugin for WebView File XHR
Universal Permissive License v1.0
139 stars 118 forks source link

Object.toString doesn't work as expected with FormData polyfil #39

Closed elliottpalermo closed 4 years ago

elliottpalermo commented 5 years ago

The FormData polyfil does not set the toStringTag symbol which will cause Object.toString to incorrectly report these objects as "[object Object]" instead of "[object FormData"]

//This will work...
var fd = new FormData();
fd.toString(); //Outputs "[object FormData"]

//this will not work
var fd = new FormData();
Object.toString.call(fd); //Outputs "[object Object]"

This causes issues when attempting to use multi-part file upload in AngularJS. If you do not specify the content type and post a form using $http, AngularJS is supposed to automatically detect the FormData and set the content type and boundry automatically.

It seems that this can be fixed by setting the Symbol correctly, something like:

  if( 'Symbol' in window && typeof Symbol() === 'symbol'){
    Object.defineProperty( __FormData.prototype, Symbol.toStringTag, {
      get : function(){
        return 'FormData';
      }
    });
  }
gvanmat commented 5 years ago

I think the formdata just needs to override the default impl of toString. There was a similar bug logged on the XMLHttpRequest polyfill.

elliottpalermo commented 5 years ago

Hello! Thanks for taking a look.

It actually looks like it already does override/implement toString here, https://github.com/oracle/cordova-plugin-wkwebview-file-xhr/blob/master/src/www/ios/formdata-polyfill.js#L197

That’s why calling (new FormData()).toString() will correctly return “[object FormData]”

The problem comes when using Object.toString.call (or apply, etc) which is what AngularJS uses to detect instances of FormData. You can see that here, https://github.com/angular/angular.js/blob/9f7144b035800c6671a981ba7e5987aeb6c9e959/src/Angular.js#L689

Object.toString looks at the special toStringTag symbol to figure out what to return. I have tested the fix I mentioned above and it fixes the issues we were seeing using multi-part file upload in AngularJS (using Cordova with WKWebView and this plugin).

elliottpalermo commented 5 years ago

Here’s the fix we are using. I would be happy to submit a pull request.

https://github.com/paychex/cordova-plugin-wkwebview-file-xhr/commit/6cf63a927fb7cf84cc891d8e648d4eff8a63d44c

manish2788 commented 5 years ago

@elliottpalermo Thanks Elliot. We are currently in process of pushing changes to the repo. We will publish the changes soon that will take care of modifying the implementation of "toString" to resolve the issue identified by you.

manish2788 commented 4 years ago

@elliottpalermo Issue has been fixed and Latest plugin version (2.1.3) is released to npm.