通过 idhttp 带进度条上传演示一下,下载和上传原理差不多,说明一下下面例子中的的idhttp 是动态创建的
第一步:添加一个StatusBar或者gauge 进度条,这2个都可以。我用的是 statusbar ,因为这个好看,属性保持默认就可。
第二步:获取上传进度需要填写idhttp中的onwork,onworkbegin,onworkend,由于idhttp是动态创建的所以需要覆盖一下。
在主窗体类中添加如下几个公有函数(函数名可随意),记住一定要是一个类的方法,否则报错。
[delphi] view plaincopyprint? procedure idhttp_onwork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64); procedure idhttp_beginwork(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64); procedure idhttp_endwork(ASender: TObject; AWorkMode: TWorkMode);
之后编写函数的实现:
[delphi] view plaincopyprint? procedure TFormMain.idhttp_onwork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64); begin ProgressBar1.Position:=AWorkCount ; end; procedure TFormMain.idhttp_beginwork(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64); begin ProgressBar1.Max:=AWorkCountMax; ProgressBar1.Min := 0; ProgressBar1.Position:=0; end; procedure TFormMain.idhttp_endwork(ASender: TObject; AWorkMode: TWorkMode); begin //ProgressBar1.Position:=ProgressBar1.Max; ProgressBar1.Position:=0;
第三步:在动态创建idhttp的时候 ,像这样写
[delphi] view plaincopyprint? idhttp1 := TIdHTTP.Create(); idhttp1.OnWork := FormMain.idhttp_onwork; idhttp1.OnWorkBegin := FormMain.idhttp_beginwork; idhttp1.OnWorkEnd := FormMain.idhttp_endwork;
好了这样就可以了,不过如果上传或下载速度太快的话就无法看到进度条在动