Node-fetch 不适用于 Electron 11,错误 resp.body.pipe 不是函数
Node-fetch not working with Electron 11, error resp.body.pipe is not a function
所以我基本上是在尝试使用 node-fetch
从 URL 下载 zip,但我收到此错误:TypeError: res.body.pipe is not a function
因为我已经将我的 Electron 应用程序版本从 "electron": "^8.2.3"
更新到 "electron": "^11.0.1"
我的下载功能好像不能用了,我也看不懂这个问题。由于我的应用程序是基于这个样板文件的,所以我在干净的 electron-react-boilerplate
上尝试了它,但我遇到了同样的错误。
我执行此图片下载的函数如下:
import React from 'react';
import fetch from 'node-fetch';
import { createWriteStream } from 'fs';
const Hello = () => {
const onDownload = async () => {
const { url } = { url: 'https://file-examples-com.github.io/uploads/2017/02/zip_10MB.zip' };
try {
const res = await fetch(url);
const { status } = res;
if (status === 200) {
const fileStream = createWriteStream(
`./downloads/archive.zip`
);
res.body.pipe(fileStream);
} else {
console.log('error');
}
res.body.on('end', () => {
console.log('end');
});
} catch (error) {
console.log(error);
}
};
return (
<div>
<h1>electron-react-boilerplate</h1>
<div className="Hello">
<button type="button" onClick={() => onDownload()}>
Download
</button>
</div>
</div>
);
};
相同的功能在旧的 electron 版本上没有问题,我在这里遗漏了什么吗?
我通过从 node-fetch
切换到 axios
找到了解决方案。我想以后其他人也会有这个问题,所以我会在这里分享我的解决方案:
try {
const res = await axios(url, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
onDownloadProgress: (progressEvent) => {
const percentage = Math.round(
(progressEvent.loaded * 100) / progressEvent.total
);
dispatch(downloadProgress());
if (percentage === 100) {
dispatch(downloadEnd());
}
},
});
const { status } = res;
if (status === 200) {
const fileStream = await createFileStream(folderPath, id);
res.body.pipe(fileStream);
} else {
dispatch(downloadAccessError());
}
} catch (error) {
dispatch(downloadError());
}
所以我基本上是在尝试使用 node-fetch
从 URL 下载 zip,但我收到此错误:TypeError: res.body.pipe is not a function
因为我已经将我的 Electron 应用程序版本从 "electron": "^8.2.3"
更新到 "electron": "^11.0.1"
我的下载功能好像不能用了,我也看不懂这个问题。由于我的应用程序是基于这个样板文件的,所以我在干净的 electron-react-boilerplate
上尝试了它,但我遇到了同样的错误。
我执行此图片下载的函数如下:
import React from 'react';
import fetch from 'node-fetch';
import { createWriteStream } from 'fs';
const Hello = () => {
const onDownload = async () => {
const { url } = { url: 'https://file-examples-com.github.io/uploads/2017/02/zip_10MB.zip' };
try {
const res = await fetch(url);
const { status } = res;
if (status === 200) {
const fileStream = createWriteStream(
`./downloads/archive.zip`
);
res.body.pipe(fileStream);
} else {
console.log('error');
}
res.body.on('end', () => {
console.log('end');
});
} catch (error) {
console.log(error);
}
};
return (
<div>
<h1>electron-react-boilerplate</h1>
<div className="Hello">
<button type="button" onClick={() => onDownload()}>
Download
</button>
</div>
</div>
);
};
相同的功能在旧的 electron 版本上没有问题,我在这里遗漏了什么吗?
我通过从 node-fetch
切换到 axios
找到了解决方案。我想以后其他人也会有这个问题,所以我会在这里分享我的解决方案:
try {
const res = await axios(url, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
onDownloadProgress: (progressEvent) => {
const percentage = Math.round(
(progressEvent.loaded * 100) / progressEvent.total
);
dispatch(downloadProgress());
if (percentage === 100) {
dispatch(downloadEnd());
}
},
});
const { status } = res;
if (status === 200) {
const fileStream = await createFileStream(folderPath, id);
res.body.pipe(fileStream);
} else {
dispatch(downloadAccessError());
}
} catch (error) {
dispatch(downloadError());
}