Chetan S
Chetan S

Reputation: 945

Find extension of file from URL

I want to find what is the extension of file in current URL. I have used
http://tol.imagingtech.com/eIL/cgi-bin/eIL.pl?ACTION=get_trans_doc&DOC=ALL&CAB_NAME=TOL_AP_2012&WHSE=18&PAYEE=23003655&INV=01235770

The extension of this file is (.pdf) how can i get this. Sometimes it will be (.doc,.txt,.jpeg) so i want the exact one.

Following is the code which i used to retrieve file extension

var extFile = Document.DocumentFilePath.Split('.');
return "Backup document." + extFile[extFile.Length-1].Trim().ToLower();

It works fine for normal local path but it fails to retrieve extension which DocumentFilePath is url.

Upvotes: 1

Views: 8499

Answers (3)

richardwhatever
richardwhatever

Reputation: 5143

To find the content type, take it from the Http response as follows:

byte[] myDataBuffer = webClient.DownloadData(fileAbsoluteUrl);
string contentType = webClient.ResponseHeaders["Content-Type"];

Upvotes: 1

Asif Mushtaq
Asif Mushtaq

Reputation: 13150

I think that there is no way to get the file type without actually getting it.

You can get the information in the response header once your request is completed.

Content-Type: image/jpeg

You can do it in C# using WebClient

 WebClient client = new WebClient();
 var url = "http://tol.imagingtech.com/eIL/cgi-bin/eIL.pl?ACTION=get_trans_doc&DOC=ALL&CAB_NAME=TOL_AP_2012&WHSE=18&PAYEE=23003655&INV=01235770";
 string data = client.DownloadString(url);
 string contentType = client.Headers["Content-Type"];

Upvotes: 6

CodeCaster
CodeCaster

Reputation: 151594

Do a HEAD request to the URL and take a look at the Content-Disposition: attachment; filename=FILENAME header if that's being used.

Upvotes: 3

Related Questions