Jump to content

Possible to send binary data via ajaxRequest?


Recommended Posts



I currently use code such as the following in a TUniHtmlFrame to send binary file data from the browser to the server:


function onFileLoadEnd(aEvent) {
  var fileContent = new Uint8Array(aEvent.target.result);
  var httpRequest = new XMLHttpRequest();
  httpRequest.open('POST', gv_UploadUrl);
  httpRequest.setRequestHeader('Content-Type', 'application/octet-stream');
This works, but has the following disadvantages:
  • Prior to running this code, I set gv_UploadUrl to a value obtained from UniSession.CallbackUrl(...MainForm()...). When onFileLoadEnd is called, it then triggers the OnAjaxEvent of the project's main form. I would prefer it to trigger the OnAjaxEvent of the TUniHtmlFrame instead.

  • Handling the event in this way seems to be prone to the __BASE64__ Ajax Error that i mentioned in a previous post. I did move all of the data processing from the OnAjaxEvent handler to a background thread, and this certainly reduces the occurrence of the __BASE64__ Ajax Error, but it still happens occasionally

Is there a way to transmit binary data from the browser to the OnAjaxEvent of a TUniHtmlFrame? Elsewhere in the code i am able to use the function ajaxRequest in Javascript to trigger the OnAjaxEvent of a TUniHtmlFrame and attach text-based data to it in the third parameter:

function handleFiles(aFiles) {
  var params = [];
  for (var i = 0, f; f = aFiles[i]; i++) {
    gv_MaxFileID = gv_MaxFileID + 1;
    f.FileID = gv_MaxFileID;
    f.ToBeDeleted = false;
    params.push("filename" + i + "=" + f.name); 
    params.push("fileID" + i + "=" + f.FileID); 
    params.push("filesize" + i + "=" + f.size);
    params.push("filetype" + i + "=" + f.type); 
  ajaxRequest(frmWizardFrame_DragDropFileUploader.hfrCode, "filesdropped", params);
But it's not clear to me how i could send binary data. Of course, i could base-64 encode the binary data and then send it as text, but that makes it 33% bigger.


Alternatively, is there some version of UniSession.CallbackUrl that returns a URL that triggers the OnAjaxEvent of a given TUniHtmlFrame, rather than a form? Then i could continue using my existing code with XMLHttpRequest.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...