[英]Alternative for fs.readFile when targeting the web using webpack
In my Electron application I am reading some application files (not user files, but actual files in the Electron application root) using fs.readFile
however this obviously won't work when packing for the web. 在我的Electron应用程序中,我正在使用fs.readFile
读取一些应用程序文件(不是用户文件,而是Electron应用程序根目录中的实际文件),但是在打包Web时显然无法正常工作。 Alternatively I tried to implement something along the lines of the following: 另外,我尝试按照以下方式实现某些功能:
function loadFile(filePath: string) {
let result = null;
const xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", filePath, false);
xmlhttp.send();
if (xmlhttp.status === 200) {
result = xmlhttp.responseText;
}
return result;
}
Unfortunately I am getting the following exception: XMLHttpRequest cannot load file:///[...]/Testfile.txt. Cross origin requests are only supported for HTTP.
不幸的是,我收到以下异常: XMLHttpRequest cannot load file:///[...]/Testfile.txt. Cross origin requests are only supported for HTTP.
XMLHttpRequest cannot load file:///[...]/Testfile.txt. Cross origin requests are only supported for HTTP.
I'd like to know which approach I can use to load the content of a "server" file and not local file (even though I am testing locally at the moment) when packing for the web. 我想知道在打包Web时可以使用哪种方法来加载“服务器”文件而不是本地文件的内容(即使我目前正在本地测试)。
My solution did not work as I have opened the application locally in my browser and thus the request protocol was file://
. 我的解决方案无法正常工作,因为我已经在浏览器中本地打开了应用程序,因此请求协议为file://
。 In order to be able to debug it I installed serve
and started a local server to test the app. 为了能够对其进行调试,我安装了serve
并启动了本地服务器来测试该应用程序。
Have your questions been answered? 您的问题已经回答了吗? If not. 如果不。 try this: 尝试这个:
if (xmlhttp.status === 200 || xmlhttp.status === 0) {
result = xmlhttp.responseText;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.