0

I have got a file from url, for example http://localhost/media/gallery/3-test2.jpeg as an image, but it could be also video file or audio file. How could I get information about what type of file is it?

So far I tried using httpClient get method.

media.service.ts

public getMedia(url: string): Observable<File> {
    return this.http.get<File>(url).pipe(
        catchError(this.api.handleError)
    );
}

media.component.ts

this.mediaApi.getMedia(url).subscribe(
    response => console.log(response)
);

But I gets only Method OPTIONS is not allowed by Access-Control-Allow-Methods in preflight response

Is that the good way to achieve what I want and if yes what should I change in my code to achieve that?

kuklyy
  • 316
  • 3
  • 13
  • Use the `HEAD` http verb to return the headers only, that'll include the `Content-Type` header. The `HttpClient` has a `head` method: https://angular.io/api/common/http/HttpClient#head – Andy Lamb Aug 01 '19 at 11:29
  • 1
    You can get answer from the following link https://stackoverflow.com/questions/50082416/get-mime-filetype-of-a-url – Ajeesh Sathyan Aug 01 '19 at 11:44

1 Answers1

0

First of all, you should parse the URL to find out the type of your file:

type = URL.substr(URL.indexOf('.')+1, URL.length);

Then, you need a function to find out the category of this type. For instance jpeg belongs to images and mp3 belongs to audios. It really depends on how many categories you have or how you would like to classify the files.

Alternatively, this package may help you as well.

  • so I should play around file extension not around file mime from request? – kuklyy Aug 01 '19 at 11:43
  • 1
    Personally, I prefer more simple solutions. If you have the URLs and just want to know the type of files, I suggest you to simply play around file extensions. Otherwise, this [link](https://stackoverflow.com/questions/50082416/get-mime-filetype-of-a-url) can help you out. – Puria Rad Jahanbani Aug 01 '19 at 11:58