Web文件图像/视频标题

时间:2014-09-26 13:47:39

标签: c# http-headers file-type http-content-length webimage

在C#中,是否可以检测文件的网址是图像还是视频?是否有这样的标题值?

我有以下代码来获取Web文件的文件大小:

System.Net.WebRequest req = System.Net.HttpWebRequest.Create("http://test.png");
req.Method = "HEAD";
using (System.Net.WebResponse resp = req.GetResponse())
{
    int ContentLength;
    if(int.TryParse(resp.Headers.Get("Content-Length"), out ContentLength))
    { 
        //Do something useful with ContentLength here 
    }
}

是否可以修改此代码以查看文件是图像还是视频?

提前致谢

2 个答案:

答案 0 :(得分:0)

您可以在回复标题中查看resp.Headers.Get("Content-Type")

例如,对于jpg文件,它将是image/jpeg

查看可用内容类型的list

答案 1 :(得分:0)

您正在寻找的是" Content-Type"头

string uri = "http://assets3.parliament.uk/iv/main-large//ImageVault/Images/id_7382/scope_0/ImageVaultHandler.aspx.jpg";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "HEAD";

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    var contentType = response.Headers["Content-Type"];

    Console.WriteLine(contentType);
}