{"action":"created","issue":{"url":"https://api.github.com/repos/n1k0/casperjs/issues/1342","labels_url":"https://api.github.com/repos/n1k0/casperjs/issues/1342/labels{/name}","comments_url":"https://api.github.com/repos/n1k0/casperjs/issues/1342/comments","events_url":"https://api.github.com/repos/n1k0/casperjs/issues/1342/events","html_url":"https://github.com/n1k0/casperjs/issues/1342","id":116656821,"number":1342,"title":"Synchronous XHR causing zero byte file downloads","user":{"login":"laggingreflex","id":2523987,"avatar_url":"https://avatars.githubusercontent.com/u/2523987?v=3","gravatar_id":"","url":"https://api.github.com/users/laggingreflex","html_url":"https://github.com/laggingreflex","followers_url":"https://api.github.com/users/laggingreflex/followers","following_url":"https://api.github.com/users/laggingreflex/following{/other_user}","gists_url":"https://api.github.com/users/laggingreflex/gists{/gist_id}","starred_url":"https://api.github.com/users/laggingreflex/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/laggingreflex/subscriptions","organizations_url":"https://api.github.com/users/laggingreflex/orgs","repos_url":"https://api.github.com/users/laggingreflex/repos","events_url":"https://api.github.com/users/laggingreflex/events{/privacy}","received_events_url":"https://api.github.com/users/laggingreflex/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-11-12T22:33:16Z","updated_at":"2015-12-07T13:38:27Z","closed_at":null,"body":"Some downloads produce zero byte files. \r\nI debugged and found the root cause to be [synchronous XHR](https://github.com/n1k0/casperjs/blob/master/modules/casper.js#L243-L244).\r\nI wrote an async alternative and it works on the same file(s), proving indeed it's the sync xhr causing the issues.\r\nThis is probably what's causing all other such (zero byte files) download issues too. #73 #1188\r\n\r\nHere's a fix:\r\n    \r\n    // this.download(downloadUrl, target); \r\n    // ^ if this doesn't work. \r\n    //   Try this:\r\n\r\n    this.thenEvaluate(function(url) {\r\n        xhr = new XMLHttpRequest();\r\n        xhr.addEventListener('load', function onTransferComplete() {\r\n            // Set a flag when load completes, we'll keep checking it\r\n            transferComplete = true;\r\n        });\r\n        xhr.open('GET', url, true); /* < ASYNC */\r\n        xhr.responseType = 'arraybuffer';\r\n        xhr.send(null);\r\n    }, downloadUrl);\r\n    this.waitFor(function check() {\r\n        return this.evaluate(function() {\r\n            return transferComplete;\r\n        });\r\n    }, function then() {\r\n        console.log('transferComplete!');\r\n        var base64encoded = this.evaluate(function() {\r\n            return btoa([].reduce.call(new Uint8Array(xhr.response), function(p, c) {\r\n                return p + String.fromCharCode(c);\r\n            }, ''));\r\n        });\r\n        /*clientutils = require('clientutils') */\r\n        /*fs = require('fs') */\r\n        var cu = clientutils.create(); \r\n        fs.write(target, cu.decode(base64encoded), 'wb');\r\n    });\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/n1k0/casperjs/issues/comments/162526709","html_url":"https://github.com/n1k0/casperjs/issues/1342#issuecomment-162526709","issue_url":"https://api.github.com/repos/n1k0/casperjs/issues/1342","id":162526709,"user":{"login":"jefleponot","id":5547697,"avatar_url":"https://avatars.githubusercontent.com/u/5547697?v=3","gravatar_id":"","url":"https://api.github.com/users/jefleponot","html_url":"https://github.com/jefleponot","followers_url":"https://api.github.com/users/jefleponot/followers","following_url":"https://api.github.com/users/jefleponot/following{/other_user}","gists_url":"https://api.github.com/users/jefleponot/gists{/gist_id}","starred_url":"https://api.github.com/users/jefleponot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jefleponot/subscriptions","organizations_url":"https://api.github.com/users/jefleponot/orgs","repos_url":"https://api.github.com/users/jefleponot/repos","events_url":"https://api.github.com/users/jefleponot/events{/privacy}","received_events_url":"https://api.github.com/users/jefleponot/received_events","type":"User","site_admin":false},"created_at":"2015-12-07T13:38:27Z","updated_at":"2015-12-07T13:38:27Z","body":"thanks @laggingreflex , very good job. Does your code work with multiple parallel downloads ?\r\n\r\nHi #mickaelandrieu, @paazmaya \r\nCould we have a casperjs \"download\" method with parameters to force asynchronous mode ?"}}