Angular - 如何在没有 fs 的情况下检查文件是否存在 [英] Angular - How to check if file exists without fs

查看:36
本文介绍了Angular - 如何在没有 fs 的情况下检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

As 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(); }

我尝试安装 file-exists package 但这使用

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.

推荐答案

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

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆