Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2012-08

fpWeb, XHR2 and arraybuffer

2 messages · 1 person · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Reimar Grabowski

Hi,

I am trying to use fpWeb in a simple cgi app to send data to a webpage. I am using XMLHttpRequest2 on the JS side. My problem is that the response is null if I set the responseType to "arraybuffer". If I do not set a responseType at all I get a response. My JS skills are not the best but I think the code is correct (at least that is how it is done in all the examples I studied). But the cgi app seems to work correctly, too. If I point my browser to the cgi app it downloads the data to a file and this data is correct. Most likely I am doing something stupid or missing something obvious but currently I don't know what to do or where to look to get this working.

The relevant code parts:
Server-side:

Procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
  AResponse: TResponse; Var Handled: Boolean);
var
  StreamResponse: TMemoryStream;
  Data: array [0..7] of byte;
begin
  StreamResponse:=TMemoryStream.Create;
  try
    Data[0]:=0;
    Data[1]:=1;
    Data[2]:=2;
    Data[3]:=3;
    Data[4]:=4;
    Data[5]:=5;
    Data[6]:=6;
    Data[7]:=7;
    StreamResponse.Write(Data, SizeOf(Data));
    AResponse.ContentType:='application/octet-stream';
    AResponse.ContentLength:=StreamResponse.Size;
    AResponse.ContentStream:=StreamResponse;
    AResponse.Code:=200;
    AResponse.SendContent;
  finally
    StreamResponse.Free;
  end;
  AResponse.ContentStream:=nil;
  Handled:=True;
end;

Client-side:

function sendXHR() {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "http://devserver/cgi-bin/cgitest", true);
  xhr.responseType = "arraybuffer";
  xhr.onload = function() {
    var arrayBuffer = xhr.response;
  }
  xhr.send();
}

Any help is appreciated.
R.

Reply by email

Reimar Grabowski
to Reimar Grabowski
I am using XMLHttpRequest2 on the JS side. My problem is that the response is null if I set the responseType to "arraybuffer".

The inspection/debugging tools of firefox/chromium report the response as null and the arrayBuffer variable as empty but actually it is not. Just creating a view (typed JS array) from the response made my example work. So there is no problem with for the noise.

R.

Reply by email