0

I am trying to read the extension of a file which is coming from a server using web services in Angular 6.

I am able to read the extensions of the file like myFile.png using filename.split('.').pop(); method and using filename.slice((filename.lastIndexOf(".") - 1 >>> 0) + 2); method.

but my problem is the file link that is coming from the server is very long in length and it is a downloadable link so that from that link we can download the actual file.

Sample link format is as follows: http://myserver.data.com/data/service/auth/yt.content.ContentHttp/viewContent/myfile.png?u8&HttpOperationItem=yt.content.ApplicationData%3A342376&ofn=myfile.png&ContentHolder=yt.flow.forum.data%3A342370&forceDownload=true

In the above link, I want to read the extension of the file which is just before the (?) operator. and the operator(?) is coming only once in the link. so that I can identified that the extension of the file is placed just before the (?) operator.

can anyone help to get the extension of the file using Angular 6.

Jayesh Vyas
  • 1,145
  • 3
  • 15
  • 35

2 Answers2

1

As you said ? will come only once so can split with ? and then select first element and again split with . and get last element.

filename.split('?')[0].split('.').pop();
ashish pal
  • 467
  • 4
  • 10
0

You can use a regex. Such as : viewContent\/\w+.([\w\d]+)

Here is an example of how to use the regex:

const regex = /viewContent\/\w+.([\w\d]+)/gm;
const str = `http://myserver.data.com/data/service/auth/yt.content.ContentHttp/viewContent/myfile.png?u8&HttpOperationItem=yt.content.ApplicationData%3A342376&ofn=myfile.png&ContentHolder=yt.flow.forum.data%3A342370&forceDownload=true`;

regex.exec(str)) !== null
Wandrille
  • 6,267
  • 3
  • 20
  • 43