File operations through byte arrays are often used in FTP. I also found these two functions when Delphi calls web service to upload and download files, which is quite useful, we recommend it to you. (Statement: not written by myself)
1. Generate a byte array File
ProcedureBytearraytofile (
ConstBytearray: tbytedynarray;
ConstFilename: string );
VaR
Count: integer;
F: file of byte;
Ptemp: pointer;
Begin
Assignfile (F, filename );
Rewrite (f );
Try
Count: = length (bytearray );
Ptemp: = @ bytearray [0];
Blockwrite (F, ptemp ^, count );
Finally
Closefile (f );
End;
End;2. Generate a byte array from the file
FunctionFiietobytearray (
ConstFilename: string): tbytedynarray;
Const
Block_size = 1024;
VaR
Bytesread, bytestowrite, Count: integer;
F: file of byte;
Ptemp: pointer;
Begin
Assignfile (F, filename );
Reset (f );
Try
Count: = filesize (f );
Setlength (result, count );
Ptemp: = @ result [0];
Bytesread: = block_size;
While(Bytesread = block_size)
Do
Begin
Bytestowrite: = min (count, block_size );
Blockread (F, ptemp ^, bytestowrite, bytesread );
Ptemp: = pointer (longint (ptemp) block_size );
Count: = count-bytesread;
End;
Finally
Closefile (f );
End;
End;