1

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.

4

3 回答 3

5

我认为没有实际获取文件类型就无法获取文件类型。

header请求完成后,您可以在响应中获取信息。

Content-Type: image/jpeg

您可以在 C# 中使用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"];
于 2012-05-11T09:54:27.247 回答
3

Content-Disposition: attachment; filename=FILENAME对 URL 发出 HEAD 请求,如果正在使用,请查看标头。

于 2012-05-11T09:57:27.817 回答
1

要查找内容类型,请从 Http 响应中获取,如下所示:

byte[] myDataBuffer = webClient.DownloadData(fileAbsoluteUrl);
string contentType = webClient.ResponseHeaders["Content-Type"];
于 2014-06-19T10:39:00.753 回答