David Schwartz 427 Posted July 18, 2023 (edited) 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: 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 July 18, 2023 by David Schwartz Share this post Link to post
David Schwartz 427 Posted July 20, 2023 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! 1 Share this post Link to post