[英]xmlHttpRequest GET for responseType arraybuffer gets empty response in electron
I am using electron version 1.7.9. 我正在使用电子版1.7.9。 I need to download a zip file in my app. 我需要在我的应用中下载一个zip文件。 Following code is used for the same. 以下代码用于相同的代码。
var urlFetch = <path to the zip file I want to download> var xhr = new XMLHttpRequest(); xhr.open('GET', urlFetch, true); xhr.responseType = "arraybuffer"; xhr.onloadend = function () { var status = xhr.status; if (status == 200) { if (undefined !== xhr.response) { successCallback(xhr.response); } else { failureCallback(); } } else { console.error("Failed to fetch " + urlFetch + ", status - " + status); failureCallback(); } }; xhr.send();
On Windows 10 as well as on MacOs HighSierra, this code always gets undefined xhr.response in spite of status being 200. 在Windows 10以及MacOs HighSierra上,尽管状态为200,但此代码始终会得到未定义的xhr.response。
xhr.responseText is populated with string, but what I need is the binary data of the zip file that I can use to store into a file for further use. xhr.responseText填充有字符串,但是我需要的是zip文件的二进制数据,可以将其存储到文件中以备将来使用。
I have also tried adding following, but with no success. 我也尝试添加以下内容,但没有成功。
xhr.setRequestHeader("Content-Type", "application/zip");
Is there anything missing in the way I am coding or is this a bug in electron/chrome etc.? 我的编码方式中是否缺少任何东西,或者这是电子/铬等中的错误?
The same code works perfectly outside of electron in a stand alone HTML. 在独立的HTML中,相同的代码可以完美地在电子之外运行。
The problem was with the node js module xmlHttpRequest which was included. 问题出在其中包含了节点js模块xmlHttpRequest。 Things started working when I removed this module. 删除此模块后,一切开始正常工作。 I was not able to see the XHR requests in the "network" tab of dev tools in my electron app. 我在电子应用程序的开发工具的“网络”选项卡中看不到XHR请求。 After removing the module, I can now see the requests and things work as desired. 删除模块后,我现在可以看到请求,并且一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.