gpt4 book ai didi

typescript - Angular - 如何在没有 fs 的情况下检查文件是否存在

转载 作者:行者123 更新时间:2023-12-04 01:50:59 24 4
gpt4 key购买 nike

fs seems not to work anymore since Angular 6
我还没有找到检查文件是否存在的替代方法。
在我的特殊情况下,我必须检查 Assets 文件夹中是否存在特定的图像文件,例如像这样:

if (fileExists('path')) { doSomething(); }

我尝试安装 file-exists package但这使用
const fs = require('fs')
const path = require('path')

这显然不受 Angular 7 支持。

有人可以帮忙吗?非常感谢。

最佳答案

谢谢你,scipper。是的,这就是我现在管理它的方式。但我希望有一个更简单的单行函数来检查这个。

这里的代码(它返回文件夹,而不是文件,所以我可以将它用于更多目的):

getFolder(subFolder: string): Observable<string> {
const folderPath = `assets/folder/${subFolder.toLocaleUpperCase()}`;
return this.httpClient
.get(`${folderPath}/file.png`, { observe: 'response', responseType: 'blob' })
.pipe(
map(response => {
return folderPath;
}),
catchError(error => {
return of('assets/folder/default');
})
);
}

关于typescript - Angular - 如何在没有 fs 的情况下检查文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53118500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com