我有一个应用程序访问内部网上的文件。用户可以将文件从位于网络上的服务器复制到本地PC。我遇到了初始连接问题。即当应用程序启动时。用户在数据库中搜索文件。当他找到一个他想下载的文件时,他点击该名称并且该应用程序下载它。 第一次下载操作大约需要8-12秒才能初始化下载并查看进度条。接下来的下载几乎是即时的。文件大小在 1 MB到15 MB之间变化。将文件从服务器复制到本地驱动器 - 什么会减慢获取文件的访问?
这里是我的代码:
const
projectFilesURL = '\\IntranetServer\Directory\filesLocation\';
procedure form1.GetSelectedFile(const fileName: string);
var
sourceFile: string;
begin
{ \\IntranetServer\Directory\filesLocation\userSelectedFile.zip}
sourceFile := projectFilesURL + fileName;
if FileExists(sourceFile) then
begin
fileCopy(fileName);
lblSearching.Hide;
AnimSearching.Hide;
end
else
MessageDlg(
'The file was not found on the server'
, mtInformation, [mbCancel], 0);
end;
end;
procedure form1.fileCopy(const sourceFile: string);
var
SourceF, DestF: file;
Buf: array [0 .. 1023] of byte;
NumRead, FSize, BytesCopied: Integer;
destinationPathandFile: string;
begin
destinationPathandFile := ExtractFilePath(ParamStr(0))
+ exportPath + sourceFile;
try
AssignFile(SourceF, projectFilesURL + sourceFile);
AssignFile(DestF, destinationPathandFile);
FileMode := 0;
Reset(SourceF, 1);
Rewrite(DestF, 1);
FSize := FileSize(SourceF);
BytesCopied := 0;
fileCopyProgress.Percent := 0;
while not Eof(SourceF) do
begin
BlockRead(SourceF, Buf, SIZEOF(Buf), NumRead);
BlockWrite(DestF, Buf, NumRead);
Inc(BytesCopied, NumRead);
fileCopyProgress.Percent := (BytesCopied * 100) div FSize;
Application.ProcessMessages;
end;
CloseFile(SourceF);
CloseFile(DestF);
except
on E: Exception do
begin
raise Exception.Create('Error occured while copying a file');
Exit;
end;
end;
我不知道哪里可能是窒息点...也许FILEEXISTS是没有必要的。 由于文件是在服务器上或不...可能抛出异常更好... 我欣赏任何建议。 谢谢,
感谢您的链接乔,这是一个很好的例子,我会看看如果我可以用我的程序来实现它。 – Greener 2009-10-25 00:44:17