Angular-如何检查没有fs的文件是否存在 [英] Angular - How to check if file exists without fs
问题描述
fs
自Angular 6起似乎不再起作用
我还没有发现检查文件是否存在的另一种方法.
在我的特殊情况下,我必须检查资产文件夹中是否存在特定的图像文件,例如像这样的东西:
As fs
seems not to work anymore since Angular 6
I haven't found an alternative way to check if a file exists.
In my special case I have to check if a specific image file exists in the assets folder, e.g. something like this:
if (fileExists('path')) { doSomething(); }
我尝试安装文件已存在包,但这使用了
I've tried to install the file-exists package but this uses
const fs = require('fs')
const path = require('path')
显然不是Angular 7支持的.
which apparently is not supported by Angular 7.
有人可以提供帮助吗?非常感谢.
Is there anybody who can help? Thanks a lot.
推荐答案
谢谢.是的,这就是我现在管理的方式.但我希望会有一个更简单的单行函数来检查这一点.
Thank you, scipper. Yes, that's the way I manage it now. But I hoped there would be a easier, one-line function to check this.
这里是代码(它返回文件夹,而不是文件,因此我可以将其用于更多目的):
Here the code (it returns the folder, not the file, so I can use it fore more purposes):
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');
})
);
}
这篇关于Angular-如何检查没有fs的文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!