I need to upload the image (directly, not a saved file) of an WebImageControl via xhr.open('POST', 'http://localhost:2001/tms/sparkle/upload');
But I'm struggling to prepare the formdata.append - content correctly in WebCore. Maybe it must be converted into a blob, but my JavaScript knowledge is not enough to manage that.
Or is there an easier way in WebCore to achieve that ?
You can mix Javascript with Pascal code and use "this" keyword in Javascript as you would "self" in Pascal. You can expand the framework without limitations from Web Core. Looping asm blocks does not work so keep it linear.
function TMyOtherClass.DoUploadComplete(Event: TEventListenerEvent): Boolean;
var
LTarget: TJSXMLHttpRequest;
begin
LTarget := TJSXMLHttpRequest(Event.target);
Result := (LTarget.Status = 200) or (LTarget.Status = 201) or (LTarget.Status = 204);
if Result then
begin
Log('DoUploadComplete: ' + DateTimeToStr(Now) + ' OK');
end else
Log('DoUploadComplete: ' + DateTimeToStr(Now) + ' FAILED');
end;
Thanks - great - this was very valueable!
Since I want to upload the img via formdata I choose this way and it uploads a file properly without errors to the planned folder.
var FileContent: TBytes;
...
FileContent:= BytesOf(WebImageControl1.Base64Image);
asm
formData = new FormData();
formData.append('doctyp', document.getElementById('exampledoctyp').value);
formData.append('emailAddress', document.getElementById('exampleInputEmail1').value);
var blob = new Blob([new Uint8Array(FileContent)], { type: 'image/png' });
formData.append('fileUpload', blob,'a.png');
end;
...
But when I try to open the saved a.png I get a message the the file is not a valid format.
So I guess I did something wrong somewhere in the conversion.
Well, this is normal. You are literally taking the base64 encoding string characters to a byte array. But you need the byte array itself which is the PNG data and not the byte array of a base64 encoded string.
In WEBLib.Utils, the function Base64ToArrayBuffer() performs this conversion.