Jump to content
David Schwartz

is this a valid, complete JSFormData attachment?

Recommended Posts

I've got this file I'm trying to attach to a POST request in TMS WEB Core as follows:

 

procedure TFile.Upload(AAction: string);
var
  f: TJSHTMLFile;
  fd: TJSFormData;
  ptrProgress, ptrComplete, ptrError, ptrAbort: pointer;
begin
  f := FileObject;
  fd := TJSFormData.new;
  fd.Append('file1',f);    <-------- the file is Base64 encoded
  FReq := TJSXMLHttpRequest.new;
  . . . event handler assignments . . .
  FReq.Open('POST',AAction, true);    
  FReq.Send(fd);
end;
procedure TFile.Upload(AAction: string);
var
  f: TJSHTMLFile;
  fd: TJSFormData;
  ptrProgress, ptrComplete, ptrError, ptrAbort: pointer;
begin
  f := FileObject;
  fd := TJSFormData.new;
  fd.Append('file1',f);    <--------
  FReq := TJSXMLHttpRequest.new;
  . . . event handler assignments . . .
  FReq.Open('POST',AAction, true);    
  FReq.Send(fd);
end;

 

This is using javascript under the hood. It's being put into a JSFormData object, which is being imported from a standard JS file.

 

I have this code in a PHP script to receive it:

 

$fn = $destinationFolder . $_GET['filename'];
$data = base64_decode($_FILES['file1']);   <-------
$file = fopen($fn, 'w');
fwrite($file, $data);
fclose($file);

 

It _seems_ to be getting uploaded, but it's not getting delivered. My web console debugger shows this:

 

image.thumb.png.013c7a8540d60772380a4010dce69d8b.png

 

First, that attachment looks empty. Or maybe the file itself is attached separately. I don't know. Does this look correct for a JS upload like this?

 

Second, is $_FILES['file1'] the right way to refer to it on the server side in PHP?

 

Would it be the same in, say, Python?

 

 

 

 

 

Edited by David Schwartz

Share this post


Link to post

The Mystery has been SOLVED!!!

 

<?php

// a lot of safety and security checks go here, along with the folder:

$destinationFolder = 'dest/folder/goes/here/';

$data = file_get_contents( $_FILES['file1']['tmp_name'] );
$fn   = $destinationFolder . $_FILES['file1']['name'];
$file = fopen($fn, 'w');
fwrite($file, $data);
fclose($file);

http_response_code(200);
echo 'File ' . $_FILES['file1']['name'] . ' uploaded to: ' . $fn;

?>

 

file_get_contents apparently tells the client to send the file to the server (this script) and it gets saved into $data, which is done in another thread or session or something, because it does not appear as part of the upload payload with the request.

 

It boiled down to getting just two lines of code correct!

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×