xdomain icon indicating copy to clipboard operation
xdomain copied to clipboard

Bug: Angular-File-Upload produces "DataCloneError" in Chrome

Open vlatkoodrljin opened this issue 10 years ago • 22 comments

I use angular and https://github.com/danialfarid/angular-file-upload

My code is almost same as: https://github.com/jpillora/xdomain/tree/gh-pages/example/angular

When i try upload file in chrome error is following:

Uncaught DataCloneError: Failed to execute 'postMessage' on 'Window': An object could not be cloned. xdomain.js:882

Chrome version: 40.0.2214.111

Any suggestion?

vlatkoodrljin avatar Feb 16 '15 15:02 vlatkoodrljin

Still unsure why this is happening. Currently debugging this with @Ganganation. I'm now thinking it might have been a Chrome change which is causing it...

Edit: Though that would be strange because http://jpillora.com/xdomain/example/formdata/ still works...

jpillora avatar Feb 16 '15 15:02 jpillora

Any progress with that?

vlatkoodrljin avatar Feb 18 '15 21:02 vlatkoodrljin

Tried debugging yesterday, can't figure out what's causing it. Will need to make a postMessage only test to attempt to see what's causing the DataCloneError error.

jpillora avatar Feb 19 '15 03:02 jpillora

The FormData example http://jpillora.com/xdomain/example/formdata/ is very similar although it works, so it's quite puzzling.

jpillora avatar Feb 19 '15 03:02 jpillora

I'm currently working on a project that uses both x-domain and Angular-File-Upload, without issues. Code is in a private repository, so I can't share it, but it more or less looks like this:

$scope.$watch('files', function(files){
  var formData = _.reduce(files, function(form, file, index){
    form.append('file', file, filenames[index]);
    return form;
  }, new FormData());

  return this.$http.post(url, formData, {
    transformRequest: angular.identity,
    headers: {
      "Content-Type": undefined
    }
  });
});

And the view looks like:

<div ngf-drop, ng-model="files"></div>

bioball avatar Jul 02 '15 00:07 bioball

Thanks though this does not work in IE89 right? I think that's where the confusion is coming from...

On Thursday, July 2, 2015, Daniel Chao [email protected] wrote:

I'm currently working on a project that uses both x-domain and Angular-File-Upload, without issues. Code is in a private repository, so I can't share it, but it more or less looks like this:

$scope.$watch('files', function(files){ var formData = _.reduce(files, function(form, file, index){ form.append('file', file, filenames[index]); return form; }, new FormData());

return this.$http.post(url, formData, { transformRequest: angular.identity, headers: { "Content-Type": undefined } }); });

And the view looks like:

— Reply to this email directly or view it on GitHub https://github.com/jpillora/xdomain/issues/135#issuecomment-117863940.

jpillora avatar Jul 02 '15 00:07 jpillora

Haven't tested in IE8/9, but wasn't this ticket created for Chrome? It definitely works in Chrome.

bioball avatar Jul 02 '15 00:07 bioball

Ah sorry, wrong ticket, there are a few of these lol

On Thursday, July 2, 2015, Daniel Chao [email protected] wrote:

Haven't tested in IE8/9, but wasn't this ticket created for Chrome? It definitely works in Chrome.

— Reply to this email directly or view it on GitHub https://github.com/jpillora/xdomain/issues/135#issuecomment-117864182.

jpillora avatar Jul 02 '15 00:07 jpillora

No problem!

2015-07-02 2:53 GMT+02:00 Jaime Pillora [email protected]:

Ah sorry, wrong ticket, there are a few of these lol

On Thursday, July 2, 2015, Daniel Chao [email protected] wrote:

Haven't tested in IE8/9, but wasn't this ticket created for Chrome? It definitely works in Chrome.

— Reply to this email directly or view it on GitHub https://github.com/jpillora/xdomain/issues/135#issuecomment-117864182.

— Reply to this email directly or view it on GitHub https://github.com/jpillora/xdomain/issues/135#issuecomment-117864367.

uteq avatar Jul 02 '15 09:07 uteq

any progress on this one? I have the same problem with angular-file-upload + xdomain in chrome 45 and IE 11.

jansepke avatar Sep 29 '15 11:09 jansepke

I have eventually stopped using angular-file-upload. I created a base64 hash of the file and used that to sends it from A to B. If you'd like I can dig up what I actually did.

uteq avatar Sep 29 '15 11:09 uteq

here I found something about it: https://github.com/azer/prova/issues/12

and this fix will solve it for me, patching the emit method:

  emit = function(args) {
    args = JSON.parse(JSON.stringify(args));
    if (jsonEncode) {
      args = JSON.stringify(args);
    }
    frame.postMessage(args, "*");
  };

jansepke avatar Sep 29 '15 11:09 jansepke

the problem is that the postMessage method can't handle functions e.g.

 window.postMessage({test:function(){}},'*')

and angular-file-upload puts a custom function on the xhr object. JSON parse + stringify will remove all functions and returns a plain object

jansepke avatar Sep 29 '15 11:09 jansepke

@Jandalf I'm having this issue too. Where do I patch the emit function? Would this be in the socket file?

empty avatar Oct 09 '15 04:10 empty

@empty you need to edit the xdomain file: https://github.com/jpillora/xdomain/blob/gh-pages/dist/xdomain.js#L856

this only the DataCloneError but I could not upload a file anymore, only do plain http POSt without a file (maybe its working for you)

jansepke avatar Oct 09 '15 08:10 jansepke

@Jandalf thanks I'll give it a try.

empty avatar Oct 09 '15 14:10 empty

@Jandalf I get the same thing as you. It actually uploads part of the file, but the file seems to be wrapped in the javascript object. I will keep looking at this and if I find a solution I'll report back, but the fact that this project is horribly maintained will likely have me looking for a better solution overall.

empty avatar Oct 09 '15 16:10 empty

@jansepke your solution works for me too. Thank you

sahanhasitha avatar Oct 24 '16 01:10 sahanhasitha

@jansepke I'm using laravel invertion to catch data from back-end. Your patch works fine. It sends post request successfully. But does not set path correctly. I'm getting back-end error "NotReadableException. Unable to read image from path (/tmp/phpddocUv)"

buddhipriya avatar Oct 24 '16 15:10 buddhipriya

I'm also using the patch suggested by @jansepke, which works fine when my ng app is on the same server as the file repo, but, as noted by @empty, when I upload from a cross origin server, files are written as ASCII text, not image data, the text being simply "[object Object]".

`locally uploaded through ng form, everything is fine: $ file test-1-local.jpg test-1-local.jpg: JPEG image data, JFIF standard 1.01

uploaded from another server: $ file test-2-cross.jpg test-2-cross.jpg: ASCII text, with no line terminators`

drogers98 avatar Nov 02 '16 18:11 drogers98

@vlatkodrljin workaround is to use angular http service like @bioball described.

@jpillora you can reproduce the problem using Upload service like here https://github.com/danialfarid/ng-file-upload/blob/master/demo/src/main/webapp/js/upload.js#L77

brndusic avatar Dec 25 '16 22:12 brndusic

@jansepke any reason why your fix worked? I do not see much difference as such, so asking.. Interestingly it has worked for me, too. @jpillora you may want to consider his patch?

args = JSON.parse(JSON.stringify(args));

anandaanv avatar May 09 '18 09:05 anandaanv