Angular2:使用http.get检查文件是否存在 [英] Angular2: check file existance with http.get

查看:201
本文介绍了Angular2:使用http.get检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的方法来检查本地计算机中是否存在*.mp3文件. 应该返回响应的 status(200、404、403等),但是它不起作用.

I have a simple method to check if a *.mp3 file in my local machine exists or not. It is supposed to return the status of the response (200, 404,403,etc), but it doesn't work.

fileExists(url){
        return this.http.get(url).timeout(2000)
                                 .map(res=>res.json())
                                 .subscribe(
                                           res=>{
                                                 console.log("res is");
                                                 console.log(res.status)
                                                 return res.status;},
                                           err =>{ 
                                                  console.log("Error is");
                                                   console.log(err.status); 
                                                   return err.status});

  }

我为此设置了2秒钟的超时时间,但是它只需要检查本地主机中的文件即可.因此,我认为,它有足够的时间来查找文件.

I set a timeout for 2 seconds for it, however it only needs to check a file in my localhost. Therefore, i think, it has enough time to find the file.

它总是返回:

Subscriber {closed: false, syncErrorValue: null, syncErrorThrown: false, syncErrorThrowable: false, isStopped: false…}

推荐答案

如果要编写一个发出GET请求状态代码的可观察对象,可以执行以下操作:

If you want to compose an observable that emits the status code of a GET request, you can do this:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/map';

this.http.get("/some-file.txt")
    .map((response) => response.status)
    .catch((error) => Observable.of(error.status || 404))
    .subscribe((status) => console.log(`status = ${status}`));

如果要基于HTTP状态代码更有效地确定文件的存在,则可以使用HEAD方法,该方法将不会检索文件的内容:

And if you want to more efficiently determine the existence of a file based on a HTTP status code, you could use the HEAD method, which won't retrieve the file's content:

this.http.head("/some-file.txt")
    .map((response) => response.status)
    .catch((error) => Observable.of(error.status || 404))
    .subscribe((status) => console.log(`status = ${status}`));

您的问题中的代码存在问题,即您正在寻找响应中的JSON内容,并且正在该内容中寻找status. status在响应本身中.

The problem with the code in your question is that you are looking for JSON content in the response and are looking for the status within that content. The status is in the response itself.

但是,要实现确定文件存在的功能,您甚至无需检查status:

However, to implement a function that determines file existence, you don't even need to examine the status:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/mapTo';
import 'rxjs/add/operator/toPromise';

getFileStatus(path: string): Promise<boolean> {
    return this.http.head(path)
        .mapTo(true)
        .catch((error) => Observable.of(false))
        .toPromise();
}

这篇关于Angular2:使用http.get检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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