spez
spez

Reputation: 469

Detect if link is downloadable file

How can I detect if the target link will trigger download, before user actually clicks it?

For example, the following link is a file:

string url="http://www.orimi.com/pdf-test.pdf"

But this one is not:

string url="https://www.google.com/"

I tried like:

Uri uri = new Uri(url);
if (uri.IsFile)
   //...

but it's gives false for the pdf link

Upvotes: 1

Views: 1835

Answers (1)

Shai Aharoni
Shai Aharoni

Reputation: 1957

You can't really know that the link you have doesn't cause a file download (before you call the URL) because even a URL without a file extension can be linked to a file.

What you can do is check if the URL contains a file extension, and this can be done using the following code:

 var uri = new Uri('https://www.google.com/');

 var fileInfo = new FileInfo(uri.AbsolutePath);
  if (!string.IsNullOrWhiteSpace(fileInfo.Extension))
  {
    //Uri has no file extension
  } 

Upvotes: 4

Related Questions