I have an application which accesses files on the intranet. The users can copy the files from the server located on the network to their local PCs. I have encoutered a problem with initial connection. i.e. when the application is started. The user searches for the file in the database. When he finds a file he wants to download he clicks on the name and the application downloads it. The first time download operation takes about 8-12 seconds just to initial the download and see the progress bar. The next downloads are almost instant. The file size varies between 1 MB to 15 MB.
Here is my code:
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;
I am not sure where could be a choking point...maybe fileExists is not necessary. Since a file is on the server or not...maybe to throw exception is better... I appreciate any suggestions. Thanks,