Delphi如何下载文件?

Delphi如何下载文件?,第1张

用indy控件组。

http用idhttp。

ftp用idftp.

通过文件 *** 作或用tstringlist.readfromfile把文本文档载进内存,然后用pos函数判断。

网上搜一搜,这样的代码到处都是

function DownloadToFile(const Url, LFile: string): Boolean

var

NetHandle: HINTERNET

UrlHandle: HINTERNET

Buffer: array[0..1024] of Char

BytesRead,WriteNumber: Cardinal

hFile: THandle

begin

Result := False

hFile := CreateFile(PChar(LFile),

GENERIC_WRITE,

0,

nil,

CREATE_ALWAYS,

FILE_ATTRIBUTE_NORMAL,

0)

if hFile = INVALID_HANDLE_VALUE then

begin

//MsgBox(0,'创建文件失败')

Exit

end

NetHandle := InternetOpen('Delphi 5.x', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0)

if Assigned(NetHandle) then

begin

UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0)

if Assigned(UrlHandle) then

{ UrlHandle valid? Proceed with download }

begin

SetFilePointer(hFile, 0, nil, FILE_BEGIN)

FillChar(Buffer, SizeOf(Buffer), 0)

repeat

//Result := Result + Buffer

FillChar(Buffer, SizeOf(Buffer), 0)

InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead)

WriteFile(hFile,Buffer, BytesRead, WriteNumber, nil)

if (BytesRead <>WriteNumber) then

begin

//MsgBox(0,'保存文件失败')

break

end

//LFileStream.Write(Buffer, BytesRead)

until BytesRead = 0

CloseHandle(hFile)

InternetCloseHandle(UrlHandle)

end

InternetCloseHandle(NetHandle)

end

end


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12085522.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存