Delphi从Internet下载文件
2011-02-26 11:26
387 查看
今天在做拍卖系统的时候,因考虑到网络状况问题,需要将拍品所有信息下载到本机,包括拍品图片,因此需要实现从Internet下载文件的功能。
下面是代码片段:
1 ..
2
3 private
4 function DownloadFile(SourceFile, DestFile: string): Boolean;
5 procedure URL_OnDownloadProgress(Sender: TDownLoadURL;
6 Progress, ProgressMax: Cardinal;
7 StatusCode: TURLDownloadStatus;
8 StatusText: String; var Cancel: Boolean) ;
9 { Private declarations }
10
11 .
12
13 function TFrameChannel.DownloadFile(SourceFile, DestFile: string): Boolean;
14 var
15 hasError: boolean;
16 begin
17 hasError:=false;
18 with TDownloadURL.Create(self) do
19 try
20 URL:=SourceFile;
21 FileName := DestFile;
22 OnDownloadProgress := URL_OnDownloadProgress;
23 ExecuteTarget(nil) ;
24 except on e: Exception do begin
25 FormMain.SetStatusInfo(e.Message);
26 Free;
27 hasError:=true;
28 end;
29 end;
30 Result := not hasError;
31 end;
32
33 procedure TFrameChannel.URL_OnDownloadProgress;
34 begin
35 Application.ProcessMessages;
36 end;
37
38 调用时:
39 ofname:='http://www.aaa.com/1.jpg';
40 nfname:='images\1.jpg';
41 if not DownloadFile(ofname, nfname) then
42 showMessage('Error')
43 end
44 showMessage('OK');
45
注:以上程序在大文件下载过程不会死锁,而且你可以在URL_OnDownloadProgress函数中用参数来处理自已的进度条以显示下载进度。因为我的程序不需要,所以没有写出来。
/article/4888947.html
下面是代码片段:
1 ..
2
3 private
4 function DownloadFile(SourceFile, DestFile: string): Boolean;
5 procedure URL_OnDownloadProgress(Sender: TDownLoadURL;
6 Progress, ProgressMax: Cardinal;
7 StatusCode: TURLDownloadStatus;
8 StatusText: String; var Cancel: Boolean) ;
9 { Private declarations }
10
11 .
12
13 function TFrameChannel.DownloadFile(SourceFile, DestFile: string): Boolean;
14 var
15 hasError: boolean;
16 begin
17 hasError:=false;
18 with TDownloadURL.Create(self) do
19 try
20 URL:=SourceFile;
21 FileName := DestFile;
22 OnDownloadProgress := URL_OnDownloadProgress;
23 ExecuteTarget(nil) ;
24 except on e: Exception do begin
25 FormMain.SetStatusInfo(e.Message);
26 Free;
27 hasError:=true;
28 end;
29 end;
30 Result := not hasError;
31 end;
32
33 procedure TFrameChannel.URL_OnDownloadProgress;
34 begin
35 Application.ProcessMessages;
36 end;
37
38 调用时:
39 ofname:='http://www.aaa.com/1.jpg';
40 nfname:='images\1.jpg';
41 if not DownloadFile(ofname, nfname) then
42 showMessage('Error')
43 end
44 showMessage('OK');
45
注:以上程序在大文件下载过程不会死锁,而且你可以在URL_OnDownloadProgress函数中用参数来处理自已的进度条以显示下载进度。因为我的程序不需要,所以没有写出来。
/article/4888947.html
相关文章推荐
- Delphi从Internet下载文件
- Delphi从Internet下载文件
- delphi 文件下载汇总
- Delphi 获取Internet缓存文件 -- FindFirstUrlCacheEntry FindNextUrlCacheEntry
- 如何从Internet上有效而稳定地下载文件
- 使用 CInternetSession 封装多线程 http 文件下载
- Delphi实现文件下载的几种方法
- delphi 文件下载汇总delphi多线程超时控
- Delphi XE2 DataSnap 上传下载文件
- delphi下载文件,delphi从http服务器上下载文件
- 用Delphi实现文件下载的几种方法
- 解决InternetReadFile下载带有中文字符的文件失败的方案
- 用 Delphi 下载网络文件
- DELPHI TDownLoadURL下载网络文件
- 如何从Internet上有效而稳定地下载文件
- Delphi IdHTTP1下载文件防止假死 (
- delphi 带进程条的文件下载
- delphi 从http 上下载文件的几种方法
- delphi 实现文件http上传下载
- delphi IdHTTP实现Get方法下载文件,断点续传