how to save large array buffer from browser
Posted: Thu Mar 07, 2024 4:12 am
hi, I try to save a large array buffer from browser to local disk, in my delphi program, i registed a funciton like this:
the js code like this
when array buffer is small , the code is work correct, but When I try to save an ArrayBuffer with a length of 12600217, the program crashes when calling Value.GetKeys, and the memory usage spikes to 1.5GB.
Does anyone know what happened? Please let me know.I would appreciate any valuable clues.
Code: Select all
TChromillaExtension = class(TCefv8HandlerOwn)
function Execute(const name: ustring; const obj: ICefv8Value;
const arguments: TCefv8ValueArray; var retval: ICefv8Value;
var AException: ustring): Boolean; override;
implatement
function TChromillaExtension .Execute(const name: ustring;
const obj: ICefv8Value; const arguments: TCefv8ValueArray;
var retval: ICefv8Value; var AException: ustring): Boolean;
var
// callback, cval, rval: ICefv8Value;
// callbackArgs: TCefV8ValueArray;
Params: array of Variant;
Method: TMethod;
I: Integer;
L: Integer;
ReturnValue: Variant;
begin
Result := False;
L := Length(arguments);
SetLength(Params, L);
if name = 'saveArrayBuffer' then
begin
SaveUInt8ArrayAsFile(arguments);
retval := TCefv8ValueRef.NewNull;
Result := True;
Exit;
end;
....
end;
procedure SaveUInt8ArrayAsFile(const arguments: TCefv8ValueArray);
var
Data: TBytes;
FileStream: TFileStream;
keys: TStrings;
I, iLength: Integer;
value: ICefv8Value;
fileName: string;
begin
fileName := arguments[0].GetStringValue;
value := arguments[1];
if (Value <> nil) and (Value.IsObject) then
begin
keys := TStringList.Create;
try
iLength := Value.GetKeys(keys);
SetLength(Data, iLength);
for I := 0 to keys.Count - 1 do
Data[I] := Value.GetValueByKey(keys[I]).GetIntValue;
FileStream := TFileStream.Create(FileName, fmCreate);
try
FileStream.WriteBuffer(Data[0], Length(Data));
finally
FileStream.Free;
SetLength(Data, 0);
end;
finally
keys.Free;
end;
end;
end;
Code: Select all
const modifiedArrayBuffer = new ArrayBuffer(originalArrayBuffer.byteLength);
const view = new Uint8Array(modifiedArrayBuffer);
view.set(new Uint8Array(originalArrayBuffer));
YChromilla.saveArrayBuffer(parseQueryString(location.search).filename, view);
Does anyone know what happened? Please let me know.I would appreciate any valuable clues.