下载文件(50分)

  • 主题发起人 主题发起人 lym830116
  • 开始时间 开始时间
L

lym830116

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure Tlym.Execute;
var
MyStream:TMemoryStream;
stream:TFilestream;
name:string;
begin
with Form1do
begin
if edit1.Text='' then
messagebox(handle,'请输入要下载的文件路径!','提示!',MB_OK)
else
begin
//IdAntiFreeze1.OnlyWhenIdle:=False;
//MyStream:=TMemoryStream.Create;
try
if savedialog1.Execute then
begin
try
//showmessage(savedialog1.FileName);
name:=savedialog1.FileName;---------这个地方出错,导致一点反映都没有,想停止程序都比较困难
stream:=Tfilestream.Create(name,fmcreate);
ProgressBar1.Position:=0;
button1.Enabled:=false;
edit1.Enabled:=false;
IdHTTP1.Get(edit1.Text,stream);
//MyStream.SaveToFile(name);
// MyStream.Free;
stream.Free;
messagebox(handle,'下载成功!','提示!',MB_OK);
button1.Enabled:=true;
edit1.Enabled:=true;
except
messagebox(handle,'请确认文件路径输入正确!','提示!',MB_OK);
end;
end;

except
messagebox(handle,'网络连接出错!请确认网络连接正常!','提示!',MB_OK);
MyStream.Free;
Exit;
end;
end;
end;
end;
不明白为什么会出错,如果不用多线程是可以的
 
注意你的写法~~>
给你一段下载代码,你可以参考一下~
//ftp方式下载
procedure TFrm_Main.FtpDownLoad(aURL, aFile: string;
bResume: Boolean);
var
tStream: TFileStream;
sName, sPass, sHost, sPort, sDir: string;
begin
if FileExists(aFile) then
tStream := TFileStream.Create(aFile, fmOpenWrite)
else
tStream := TFileStream.Create(aFile, fmCreate);
//建立文件流
GetFTPParams(aURL, sName, sPass, sHost, sPort, sDir);
with IdFTP1do
try
if Connected then
Disconnect;
//重新连接
Username := sName;
Password := sPass;
Host := sHost;
Port := StrToInt(sPort);
Connect;
except
exit;
end;

IdFTP1.ChangeDir(sDir);
//改变目录
BytesToTransfer := IdFTP1.Size(aFile);
try
if bResume then
//续传
begin
tStream.Position := tStream.Size;
IdFTP1.Get(aFile, tStream, True);
end
else
begin
IdFTP1.Get(aFile, tStream, False);
end;
finally
tStream.Free;
end;
end;

//http方式下载
procedure TFrm_Main.HttpDownLoad(aURL, aFile: string;
bResume: Boolean);
var
tStream: TFileStream;
begin
try
//如果文件已经存在
if FileExists(aFile) then
tStream := TFileStream.Create(aFile, fmOpenWrite)
else
tStream := TFileStream.Create(aFile, fmCreate);
if bResume then
//续传方式
begin
IdHTTP1.Request.ContentRangeStart := tStream.Size - 1;
tStream.Position := tStream.Size - 1;
//移动到最后继续下载
IdHTTP1.Head(aURL);
IdHTTP1.Request.ContentRangeEnd := IdHTTP1.Response.ContentLength;
end
else
//覆盖或新建方式
begin
IdHTTP1.Request.ContentRangeStart := 0;
end;
try
IdHTTP1.Get(aURL, tStream);
//开始下载
finally
tStream.Free;
end;
except
on E: Exceptiondo
begin
if (Pos('Operation aborted', E.Message) >= 0) and AbortTransfer then
begin
E.Message := '已被用户中断';
end;
Application.MessageBox(PChar('升级过程中出现了错误了,错误信息如下:' + #13
+ #13 + E.Message), PChar('系统提示'), Mb_OK + MB_ICONERROR);
WriteErrLog('升级过程中出现了错误了,错误信息如下:' + E.Message);
CopyFile(PChar(ExtractFilePath(ParamStr(0)) + 'Bak/KQSys.exe'),
PChar(ExtractFilePath(ParamStr(0))), False);
Abort;
end;
end;
end;
 
我自己发现了。就是在stream:=Tfilestream.Create(savedialog1.FileName,fmcreate);
但是我不太明白为什么用name就不可以,我是将name:=savedialog1.FileName
 
接受答案了.
 
后退
顶部