我正在尝试查找服务器上文件的文件大小。我从this guy获得的以下代码为您自己的服务器完成了这些代码:
string MyFile = "~/photos/mymug.gif";
FileInfo finfo = new FileInfo(Server.MapPath(MyFile));
long FileInBytes = finfo.Length;
long FileInKB = finfo.Length / 1024;
Response.Write("File Size: " + FileInBytes.ToString() +
" bytes (" + FileInKB.ToString() + " KB)");
有效。但是,我想找到文件大小,例如:
string MyFile = "http://www.google.com/intl/en_ALL/images/logo.gif";
FileInfo finfo = new FileInfo(MyFile);
然后我得到一个令人生畏的错误,说URI formats are not supported.
如何使用ASP.NET查找Google徽标的文件大小?
答案 0 :(得分:4)
您可以使用WebRequest
类向服务器发出HTTP请求并读取Content-Length
标头(您可以使用HTTP HEAD
方法来完成它)。但是,并非所有Web服务器都使用Content-Length
标头进行响应。在这些情况下,您必须接收所有数据才能获得大小。
答案 1 :(得分:0)
要获得此值,您必须先在本地下载文件,然后才能使用标准方法来获取其大小。