可能重复:
programatically get the file size from a remote file using delphi, before download it.如何比较本地文件与网上文件的大小?
说我有一个本地文件:
C:\ file.txt的
而一个网站:
我如何检查大小是否是不同的,如果他们是不同的,那么//做些什么?
谢谢。
可能重复:
programatically get the file size from a remote file using delphi, before download it.如何比较本地文件与网上文件的大小?
说我有一个本地文件:
C:\ file.txt的
而一个网站:
我如何检查大小是否是不同的,如果他们是不同的,那么//做些什么?
谢谢。
要获取互联网上的文件的文件大小,请
function WebFileSize(const UserAgent, URL: string): cardinal;
var
hInet, hURL: HINTERNET;
len: cardinal;
index: cardinal;
begin
result := cardinal(-1);
hInet := InternetOpen(PChar(UserAgent),
INTERNET_OPEN_TYPE_PRECONFIG,
nil,
nil,
0);
index := 0;
if hInet <> nil then
try
hURL := InternetOpenUrl(hInet, PChar(URL), nil, 0, 0, 0);
if hURL <> nil then
try
len := sizeof(result);
if not HttpQueryInfo(hURL,
HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER,
@result,
len,
index) then
RaiseLastOSError;
finally
InternetCloseHandle(hURL);
end;
finally
InternetCloseHandle(hInet)
end;
end;
例如,你可以尝试
ShowMessage(IntToStr(WebFileSize('Test Agent',
'http://privat.rejbrand.se/test.txt')));
若要获得本地文件的大小,最简单的方法是到FindFirstFile
就可以了,并且读了TSearchRec
。稍微更优雅,虽然是
function GetFileSize(const FileName: string): cardinal;
var
f: HFILE;
begin
result := cardinal(-1);
f := CreateFile(PChar(FileName),
GENERIC_READ,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if f <> 0 then
try
result := Windows.GetFileSize(f, nil);
finally
CloseHandle(f);
end;
end;
现在你可以做
if GetFileSize('C:\Users\Andreas Rejbrand\Desktop\test.txt') =
WebFileSize('UA', 'http://privat.rejbrand.se/test.txt') then
ShowMessage('The two files have the same size.')
else
ShowMessage('The two files are not of the same size.')
注:如果你的情况是不够的,使用32位来表示文件大小,你需要对上述两个函数做一些小的修改。
谢谢,正是我需要。 – Rosenberg 2011-05-13 19:18:08
+1对于使用“标准”Windows互联网电话 – 2011-05-14 09:47:43
你有很好的程序,谢谢分享。我会问一个类似的问题,但搜索带来了这个问题。做得好 :) – 2011-05-15 00:02:11
您可以为该文件发出HTTP HEAD请求并检查Content-Length标头。
检查此问题http://stackoverflow.com/questions/2184015/programatically-get-the-file-size-from-a-remote-file-using-delphi-before-downloa – RRUZ 2011-05-13 18:15:14