Datasnap dual-cache file download

Source: Internet
Author: User

Procedure tfrmmain. btnupdatefilesclick (Sender: tobject );
Begin
Try
If assigned (gexemm) then
Freeandnil (gexemm );
If assigned (ginimm) then
Freeandnil (ginimm );

Gexemm: = tfilestream. Create (extractfilepath (application. exename) +
'Download \ jlpos.exe ', fmopenread );
Ginimm: = tfilestream. Create (extractfilepath (application. exename) +
'Download \ Client. ini ', fmopenread );

Gexemm2.clear;
Ginimm2.clear;
Gexemm2.copyfrom (gexemm, 0 );
Ginimm2.copyfrom (ginimm, 0 );

Freeandnil (gexemm );
Freeandnil (ginimm );
Except
On E: exception do
Begin
Gsyslog. writelog ('btnupdatefilesclick: '+ E. Message );
Exit;
End;
End;
End;

Function tservermethods1.downloadfile (const filename: string): tstream;
VaR
F: string;
Begin
Result: = nil;
Try
F: = extractfilepath (application. exename) + 'Download \ '+ filename;
If not fileexists (f) then
Exit;
Result: = tmemorystream. Create;
If lowercase (filename) = 'jlpos.exe 'then
Result. copyfrom (gexemm2, 0)
Else if lowercase (filename) = 'client. ini 'then
Result. copyfrom (ginimm2, 0 );
Result. Position: = 0;
Except
On E: exception do
Begin
Gsyslog. writelog ('downloadfile: '+ E. Message );
Exit;
End;
End;
End;

Function tdmcommonfun. downloadfile (const filename: string): Boolean;
VaR
A: tservermethods1client;
INI: Tinifile;
Stream, Ms: tstream;
Buffer: tbytes;
Readcount: integer;
Const
Bufsize = $ f000;
Begin
Result: = false;
If (not tryconnectappserver) or (filename = '') then
Exit;
A: = tservermethods1client. Create (sqlconnection1.dbxconnection );
MS: = tmemorystream. Create;
Try
Stream: = A. downloadfile (filename );
If stream. size =-1 then
Begin
Setlength (buffer, bufsize );
Repeat
Readcount: = stream. Read (buffer [0], bufsize );
If readcount> 0 then
Ms. writebuffer (buffer [0], readcount );
If readcount <bufsize then
Break;
Until readcount <bufsize;
End
Else
Begin
Ms. copyfrom (stream, 0 );
End;
// Delete BAK files
If fileexists (extractfilepath (application. exename) + filename + 'bak') then
Deletefile (pwidechar (extractfilepath (application. exename) + filename
+ 'Bak '));
// Rename an existing file
If fileexists (extractfilepath (application. exename) + filename) then
Begin
Renamefile (extractfilepath (application. exename) + filename,
Extractfilepath (application. exename) + filename + 'bak ');
End;
// Download the latest file
Tmemorystream (MS). savetofile (extractfilepath (application. exename) +
Filename );
// Update the local version
INI: = Tinifile. Create (extractfilepath (application. exename) + 'client. ini ');
Try
INI. writeinteger (filename, 'ver ', getver (filename ));
Finally
INI. Free;
End;
Finally
A. Free;
Ms. Free;
End;
Result: = true;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.