jpillora / xdomain

A pure JavaScript CORS alternative
https://jpillora.com/xdomain/
3.12k stars 270 forks source link

Upload File in IE8/9 or Safari doesn't work #73

Closed Capellaro closed 10 years ago

Capellaro commented 10 years ago

I am trying to upload files using XDomain and the angular-upload-file https://github.com/danialfarid/angular-file-upload .

It is working nicely in Chrome and Firefox but nothing happens when submitting the exact same form in IE or Safari. No request are even send so I can not debug it...

Even your example at http://jpillora.com/xdomain/example/formdata/ doesn't work on these browsers.

jpillora commented 10 years ago

what versions of IE / Safari?

On Tue, May 13, 2014 at 9:30 PM, AlexCapi notifications@github.com wrote:

I am trying to upload files using XDomain and the angular-upload-file https://github.com/danialfarid/angular-file-upload .

It is working nicely in Chrome and Firefox but nothing happens when submitting the exact same form in IE or Safari. No request are even send so I can not debug it...

Even your example at http://jpillora.com/xdomain/example/formdata/doesn't work on these browsers.

— Reply to this email directly or view it on GitHubhttps://github.com/jpillora/xdomain/issues/73 .

Capellaro commented 10 years ago

I have Safari Version 6.1.3 and IE version 9.0.8112 for instance.

Do you have the same issue?

jpillora commented 10 years ago

Testing safari now, looks like its the same in safari 7, looking into it...

On Tue, May 13, 2014 at 9:33 PM, AlexCapi notifications@github.com wrote:

I have Safari Version 6.1.3 and IE version 9.0.8112 for instance

— Reply to this email directly or view it on GitHubhttps://github.com/jpillora/xdomain/issues/73#issuecomment-42943993 .

jpillora commented 10 years ago

so this fixed for safari, not sure if it can work in IE9, needs Blob FormData and FileReader. Are those supported?

Capellaro commented 10 years ago

Thanks for the safari's fix it is working like a charm. Indeed I am not sure Blob FormData and FileReader are supported by IE, I must give it a look

Capellaro commented 10 years ago

I have the following error when accessing the page that implements the upload file directive : Adobe Flash Player need to be installed. To check ahead use "FileApi.hasFlash"<input value="" type="file" ng-file-select="onFileSelect($files)" Refering to DanialFarid on this post https://github.com/danialfarid/angular-file-upload/issues/113 : By the way this won't work for IE8-9 FileAPI, since it is using flash to send the request not XMLHttpRequest so the cross domain policy will not allow you to upload unless you setup crossdomain.xml file on your server.

jpillora commented 10 years ago

Yep so the crossdomain.xml works similar to the proxy.html file, needs to be on the target domain. probably your best bet if you need ie cross domain Ajax file uploads

On Tuesday, May 13, 2014, AlexCapi notifications@github.com wrote:

I have the following error when accessing the page that implements the upload file directive : Adobe Flash Player need to be installed. To check ahead use "FileApi.hasFlash"<input value="" type="file" ng-file-select="onFileSelect($files)" Refering to DanialFarid on this post danialfarid/angular-file-upload#113https://github.com/danialfarid/angular-file-upload/issues/113: By the way this won't work for IE8-9 FileAPI, since it is using flash to send the request not XMLHttpRequest so the cross domain policy will not allow you to upload unless you setup crossdomain.xml file on your server.

— Reply to this email directly or view it on GitHubhttps://github.com/jpillora/xdomain/issues/73#issuecomment-42950265 .

sgarbesi commented 9 years ago

I'm having a similar issue. This is the error I'm getting:

XDomain - v0.6.15

SCRIPT87: Invalid argument. 
xdomain.min.js, line 3 character 4664

Script:

// XDomain - v0.6.15 - https://github.com/jpillora/xdomain
// Jaime Pillora <dev@jpillora.com> - MIT Copyright 2014
(function(a,b){(function(a,b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};q=a.document,d="before",c="after",l="readyState",k="addEventListener",j="removeEventListener",g="dispatchEvent",o="XMLHttpRequest",h="FormData",m=["load","loadend","loadstart"],e=["progress","abort","error","timeout"],(w=Array.prototype).indexOf||(w.indexOf=function(a){var b,c,d,e;for(b=d=0,e=this.length;e>d;b=++d)if(c=this[b],c===a)return b;return-1}),u=function(a,b){return Array.prototype.slice.call(a,b)},s=function(a,b){var c,d;for(c in a)if(d=a[c],"returnValue"!==c)try{b[c]=a[c]}catch(e){}return b},t=function(a,b,c){var d,e,f,h;for(e=function(a){return function(d){var e,f,h;e={};for(f in d)"returnValue"!==f&&(h=d[f],e[f]=h===b?c:h);return c[g](a,e)}},f=0,h=a.length;h>f;f++)d=a[f],b["on"+d]=e(d)},r=function(a){var b;if(null!=q.createEventObject)return b=q.createEventObject(),b.type=a,b;try{return new Event(a)}catch(c){return{type:a}}},f=function(a){var c,d,e;return d={},e=function(a){return d[a]||[]},c={},c[k]=function(a,c,f){d[a]=e(a),d[a].indexOf(c)>=0||(f=f===b?d[a].length:f,d[a].splice(f,0,c))},c[j]=function(a,b){var c;c=e(a).indexOf(b),-1!==c&&e(a).splice(c,1)},c[g]=function(){var d,f,g,h,i,j,k,l;for(d=u(arguments),f=d.shift(),a||(d[0]=s(d[0],r(f))),h=c["on"+f],h&&h.apply(b,d),l=e(f).concat(e("*")),g=j=0,k=l.length;k>j;g=++j)i=l[g],i.apply(b,d)},a&&(c.listeners=function(a){return u(e(a))},c.on=c[k],c.off=c[j],c.fire=c[g],c.once=function(a,b){var d;return d=function(){return c.off(a,d),b.apply(null,arguments)},c.on(a,d)},c.destroy=function(){return d={}}),c},v=f(!0),v.EventEmitter=f,v[d]=function(a,b){if(a.length<1||a.length>2)throw"invalid hook";return v[k](d,a,b)},v[c]=function(a,b){if(a.length<2||a.length>3)throw"invalid hook";return v[k](c,a,b)},v.enable=function(){a[o]=n},v.disable=function(){a[o]=v[o]},p=v.headers=function(a,b){var c,d,e,f,g,h,i,j,k;switch(null==b&&(b={}),typeof a){case"object":d=[];for(e in a)g=a[e],f=e.toLowerCase(),d.push(""+f+": "+g);return d.join("\n");case"string":for(d=a.split("\n"),i=0,j=d.length;j>i;i++)c=d[i],/([^:]+):\s*(.+)/.test(c)&&(f=null!=(k=RegExp.$1)?k.toLowerCase():void 0,h=RegExp.$2,null==b[f]&&(b[f]=h));return b}},i=a[h],i&&(v[h]=i,a[h]=function(a){var b;this.fd=a?new i(a):new i,this.form=a,b=[],Object.defineProperty(this,"entries",{get:function(){var c;return c=a?u(a.querySelectorAll("input,select")).filter(function(a){var b;return"checkbox"!==(b=a.type)&&"radio"!==b||a.checked}).map(function(a){return[a.name,"file"===a.type?a.files:a.value]}):[],c.concat(b)}}),this.append=function(a){return function(){var c;return c=u(arguments),b.push(c),a.fd.append.apply(a.fd,c)}}(this)}),v[o]=a[o],n=a[o]=function(){var b,i,j,n,q,r,u,w,y,z,A,B,C,D,E;return E=new v[o],q=!1,B=!1,y={},y.headers={},y.headerNames={},z={},z.headers={},w=function(){var a,b,c,d;z.status=E.status,z.statusText=E.statusText,d=p(E.getAllResponseHeaders());for(a in d)c=d[a],z.headers[a]||(b=a.toLowerCase(),z.headers[b]=c)},u=function(){try{z.text=E.responseText}catch(a){}try{z.xml=E.responseXML}catch(a){}z.data=E.response||z.text},D=function(){n.status=z.status,n.statusText=z.statusText},C=function(){z.hasOwnProperty("text")&&(n.responseText=z.text),z.hasOwnProperty("xml")&&(n.responseXML=z.xml),n.response=z.data||null},j=function(a){for(;a>b&&4>b;)n[l]=++b,1===b&&n[g]("loadstart",{}),2===b&&D(),4===b&&(D(),C()),n[g]("readystatechange",{}),4===b&&setTimeout(i,0)},i=function(){q||n[g]("load",{}),n[g]("loadend",{}),q&&(n[l]=0)},b=0,A=function(a){var b,d;return 4!==a?void j(a):(b=v.listeners(c),d=function(){var a;return b.length?(a=b.shift(),2===a.length?(a(y,z),d()):3===a.length&&y.async?a(y,z,d):d()):j(4)},void d())},n=y.xhr=f(),E.onreadystatechange=function(){try{2===E[l]&&w()}catch(a){}4===E[l]&&(B=!1,w(),u()),A(E[l])},r=function(){q=!0},n[k]("error",r),n[k]("timeout",r),n[k]("abort",r),n[k]("progress",function(){3>b?A(3):n[g]("readystatechange",{})}),t(e,E,n),("withCredentials"in E||v.addWithCredentials)&&(n.withCredentials=!1),n.status=0,n.open=function(a,b,c,d,e){y.method=a,y.url=b,y.async=c!==!1,y.user=d,y.pass=e,A(1)},n.send=function(b){var c,e,f,g,i,j,k,l;for(l=["type","timeout","withCredentials"],j=0,k=l.length;k>j;j++)e=l[j],f="type"===e?"responseType":e,f in n&&(y[e]=n[f]);y.body=b,i=function(){var b,c,d,g,i,j;for(B=!0,E.open(y.method,y.url,y.async,y.user,y.pass),i=["type","timeout","withCredentials"],d=0,g=i.length;g>d;d++)e=i[d],f="type"===e?"responseType":e,e in y&&(E[f]=y[e]);j=y.headers;for(b in j)c=j[b],E.setRequestHeader(b,c);a[h]&&y.body instanceof a[h]&&(y.body=y.body.fd),E.send(y.body)},c=v.listeners(d),(g=function(){var a,b;return c.length?(a=function(a){return"object"!=typeof a||"number"!=typeof a.status&&"number"!=typeof z.status?void g():(s(a,z),x.call(a,"data")<0&&(a.data=a.response||a.text),void A(4))},a.head=function(a){return s(a,z),A(2)},a.progress=function(a){return s(a,z),A(3)},b=c.shift(),1===b.length?a(b(y)):2===b.length&&y.async?b(y,a):a()):i()})()},n.abort=function(){B?E.abort():n[g]("abort",{})},n.setRequestHeader=function(a,b){var c,d;c=null!=a?a.toLowerCase():void 0,d=y.headerNames[c]=y.headerNames[c]||a,y.headers[d]&&(b=y.headers[d]+", "+b),y.headers[d]=b},n.getResponseHeader=function(a){var b;return b=null!=a?a.toLowerCase():void 0,z.headers[b]},n.getAllResponseHeaders=function(){return p(z.headers)},E.overrideMimeType&&(n.overrideMimeType=function(){return E.overrideMimeType.apply(E,arguments)}),E.upload&&(n.upload=y.upload=f(),t(e.concat(m),E.upload,n.upload)),n},"function"==typeof this.define&&this.define.amd?define("xhook",[],function(){return v}):(this.exports||this).xhook=v}).call(this,a);var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N;C=null,g=function(a){var b,c;null===C&&(C={},r());for(b in a)c=a[b],x("adding slave: "+b),C[b]=c},n={},o=function(a,b){var c;return n[a]?n[a]:(c=l.createElement("iframe"),c.id=c.name=p(),x("creating iframe "+c.id),c.src=""+a+b,c.setAttribute("style","display:none;"),l.body.appendChild(c),n[a]=c.contentWindow)},r=function(){var a,b,c;return b=function(a,b){var c,d,e,f,g;return e=a[0],f=a[1],c=t(f,"Blob"),d=t(f,"File"),c||d?(g=new FileReader,g.onload=function(){return a[1]=null,d&&(a[2]=f.name),b(["XD_BLOB",a,this.result,f.type])},g.readAsArrayBuffer(f),1):0},a=function(a,c){var d;a.forEach(function(b,c){var d,e,f,g,h;if(e=b[0],f=b[1],t(f,"FileList"))for(a.splice(c,1),g=0,h=f.length;h>g;g++)d=f[g],a.splice(c,0,[e,d])}),d=0,a.forEach(function(e,f){d+=b(e,function(b){a[f]=b,0===--d&&c()})}),0===d&&c()},c=function(b,c){var d,e,f;return c.on("xhr-event",function(){return b.xhr.dispatchEvent.apply(null,arguments)}),c.on("xhr-upload-event",function(){return b.xhr.upload.dispatchEvent.apply(null,arguments)}),e=G(b),e.headers=b.headers,b.withCredentials&&(e.credentials=l.cookie),f=function(){return c.emit("request",e)},b.body&&(e.body=b.body,t(e.body,"FormData"))?(d=e.body.entries,e.body=["XD_FD",d],void a(d,f)):void f()},K.addWithCredentials=!0,K.before(function(a,b){var d,e,f;return e=B(a.url),e&&e.origin!==k?C[e.origin]?(x("proxying request to slave: '"+e.origin+"'"),a.async===!1?(I("sync not supported"),b()):(d=o(e.origin,C[e.origin]),f=h(d),f.on("response",function(a){return b(a),f.close()}),a.xhr.addEventListener("abort",function(){return f.emit("abort")}),void(f.ready?c(a,f):f.once("ready",function(){return c(a,f)})))):(e&&x("no slave matching: '"+e.origin+"'"),b()):b()})},z=null,f=function(a){var b,c;null===z&&(z={},s());for(b in a)c=a[b],x("adding master: "+b),z[b]=c},s=function(){return v(function(a,b){var c,d,e,f;"null"===a&&(a="*"),e=null;for(c in z){f=z[c];try{if(d=H(c),d.test(a)){e=H(f);break}}catch(g){}}return e?(b.once("request",function(a){var c,d,f,g,h,i,j,k,l,m,n;if(x("request: "+a.method+" "+a.url),i=B(a.url),!i||!e.test(i.path))return I("blocked request to path: '"+i.path+"' by regex: "+e),void b.close();k=new XMLHttpRequest,k.open(a.method,a.url),k.addEventListener("*",function(a){return b.emit("xhr-event",a.type,G(a))}),k.upload&&k.upload.addEventListener("*",function(a){return b.emit("xhr-upload-event",a.type,G(a))}),b.once("abort",function(){return k.abort()}),k.onreadystatechange=function(){var a;if(4===k.readyState){a={status:k.status,statusText:k.statusText,data:k.response,headers:K.headers(k.getAllResponseHeaders())};try{a.text=k.responseText}catch(c){}return b.emit("response",a)}},a.withCredentials&&(a.headers["XDomain-Cookie"]=a.credentials),a.timeout&&(k.timeout=a.timeout),a.type&&(k.responseType=a.type),n=a.headers;for(h in n)j=n[h],k.setRequestHeader(h,j);if(a.body instanceof Array&&"XD_FD"===a.body[0]){for(g=new K.FormData,f=a.body[1],l=0,m=f.length;m>l;l++)c=f[l],"XD_BLOB"===c[0]&&4===c.length&&(d=new Blob([c[2]],{type:c[3]}),c=c[1],c[1]=d),g.append.apply(g,c);a.body=g}k.send(a.body||null)}),void x("slave listening for requests on socket: "+b.id)):void I("blocked request from: '"+a+"'")}),a===a.parent?I("slaves must be in an iframe"):a.parent.postMessage("XDPING_"+d,"*")},A=function(b){return l.addEventListener?a.addEventListener("message",b):a.attachEvent("onmessage",b)},e="XD_CHECK",q=null,E={},u=!0,F=function(){return A(function(a){var c,e,f,g;if(c=a.data,"string"==typeof c){if(/^XDPING(_(V\d+))?$/.test(c)&&RegExp.$2!==d)return I("your master is not compatible with your slave, check your xdomain.js version");if(/^xdomain-/.test(c))c=c.split(",");else if(u)try{c=JSON.parse(c)}catch(h){return}}if(c instanceof Array&&(f=c.shift(),/^xdomain-/.test(f)&&(g=E[f],null!==g))){if(g===b){if(!q)return;g=j(f,a.source),q(a.origin,g)}e="string"==typeof c[1]?": '"+c[1]+"'":"",x("receive socket: "+f+": '"+c[0]+"'"+e),g.fire.apply(g,c)}})},j=function(a,b){var d,f,g,h,i,j;return i=!1,j=E[a]=K.EventEmitter(!0),j.id=a,j.once("close",function(){return j.destroy(),j.close()}),h=[],j.emit=function(){var b,c;b=D(arguments),c="string"==typeof b[1]?": '"+b[1]+"'":"",x("send socket: "+a+": "+b[0]+c),b.unshift(a),i?g(b):h.push(b)},g=function(a){u&&(a=JSON.stringify(a)),b.postMessage(a,"*")},j.close=function(){j.emit("close"),x("close socket: "+a),E[a]=null},j.once(e,function(b){for(u="string"==typeof b,i=j.ready=!0,j.emit("ready"),x("ready socket: "+a+" (emit #"+h.length+" pending)");h.length;)g(h.shift())}),f=0,d=function(){return function(){b.postMessage([a,e,{}],"*"),i||(f++===J.timeout/c?I("Timeout waiting on iframe socket"):setTimeout(d,c))}}(this),setTimeout(d),x("new socket: "+a),j},h=function(a){var b;return b=j(p(),a)},v=function(a){q=a},K=(this.exports||this).xhook,J=function(a){a&&(a.masters&&f(a.masters),a.slaves&&g(a.slaves))},J.masters=f,J.slaves=g,J.debug=!1,J.timeout=15e3,c=100,l=a.document,w=a.location,k=J.origin=w.protocol+"//"+w.host,p=function(){return"xdomain-"+Math.round(Math.random()*Math.pow(2,32)).toString(16)},D=function(a,b){return Array.prototype.slice.call(a,b)},i=a.console||{},y=function(a){return function(b){b="xdomain ("+k+"): "+b,a in J&&J[a](b),("log"!==a||J.debug)&&(a in i?i[a](b):"warn"===a&&alert(b))}},x=y("log"),I=y("warn"),N=["postMessage","JSON"];for(L=0,M=N.length;M>L;L++)if(m=N[L],!a[m])return void I("requires '"+m+"' and this browser does not support it");t=function(b,c){return c in a?b instanceof a[c]:!1},d="V1",B=J.parseUrl=function(a){return/^((https?:)?\/\/[^\/\?]+)(\/.*)?/.test(a)?{origin:(RegExp.$2?"":w.protocol)+RegExp.$1,path:RegExp.$3}:(x("failed to parse absolute url: "+a),null)},H=function(a){var b;return a instanceof RegExp?a:(b=a.toString().replace(/\W/g,function(a){return"\\"+a}).replace(/\\\*/g,".*"),new RegExp("^"+b+"$"))},G=function(a){var b,c,d,e;b={};for(c in a)"returnValue"!==c&&(d=a[c],"function"!=(e=typeof d)&&"object"!==e&&(b[c]=d));return b},function(){var a,b,c,d,e,h,i,j,k,m,n;for(a={debug:function(a){return"string"==typeof a?J.debug="false"!==a:void 0},slave:function(a){var b,c;if(a&&(b=B(a)))return c={},c[b.origin]=b.path,g(c)},master:function(a){var b,c;if(a&&(c="*"===a?{origin:"*",path:"*"}:B(a)))return b={},b[c.origin]=c.path.replace(/^\//,"")?c.path:"*",f(b)}},m=l.getElementsByTagName("script"),h=0,j=m.length;j>h;h++)if(e=m[h],/xdomain/.test(e.src))for(n=["","data-"],i=0,k=n.length;k>i;i++){d=n[i];for(c in a)(b=a[c])(e.getAttribute(d+c))}}(),F(),"function"==typeof this.define&&this.define.amd?define("xdomain",["xhook"],function(a){return K=a,J}):(this.exports||this).xdomain=J}).call(this,window);
mwcz commented 9 years ago

Same here. Only in IE8. When I run IE11 in IE8 mode, the debugger stalls here: https://github.com/jpillora/xdomain/blob/gh-pages/dist/xdomain.js#L470

It's trying to set a header "Slave-Cookie" to the value "" (empty string). I'm not sure why, as I have specified xdomain.cookies on both the master and slave (I wasn't sure which, so I did it on both). That's when IE8 throws "SCRIPT87. Invalid argument". I don't understand why xdomain is trying to set a header called "Slave-Cookie" when I set the slave cookie name to "Cookie".

In my case I'm not trying to upload a file, just fetch some JSON.