ASP.NET/C# - 从其他服务器检测文件大小? [英] ASP.NET/C# - Detect File Size from Other Server?

查看:231
本文介绍了ASP.NET/C# - 从其他服务器检测文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个服务器上的文件的文件大小。下面的代码,我从得到这个家伙完成了为你自己的服务器:

I'm trying to find the file size of a file on a server. The following code I got from this guy accomplishes that for your own server:

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)");



它的工作原理。不过,我想找到的文件大小,例如:

It works. However, I want to find the filesize of, for example:

string MyFile = "http://www.google.com/intl/en_ALL/images/logo.gif";

FileInfo finfo = new FileInfo(MyFile);



然后我得到一个讨厌的错误说 URI格式不被支持。

如何才能找到谷歌的logo与ASP.NET文件的大小?

How can I find the file size of Google's logo with ASP.NET?

推荐答案

您可以使用的WebRequest 类发出HTTP请求到服务器并阅读内容长度头(你大概可以使用HTTP HEAD 方法来实现它)。然而,并非所有Web服务器以的Content-Length 头回应。在这种情况下,你必须接受的所有数据,以获得大小。

You can use the WebRequest class to issue an HTTP request to the server and read the Content-Length header (you could probably use HTTP HEAD method to accomplish it). However, not all Web servers respond with a Content-Length header. In those cases, you have to receive all data to get the size.

这篇关于ASP.NET/C# - 从其他服务器检测文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆