0

Need a little assistance in using php to access blob data sent from javascript post. Here is the method I used to attach blob to formdata -

var formData = new FormData();
formData.append('jsonData', 
                new Blob([JSON.stringify(
                    {"action":"readString", 
                    "details":{"string":"string1"}
                    })
                ],
                {type:'application/json'})
            );

Previously I only used $_POST method, but I am having no luck when sending file as blob.

Also, is it necessary to include "{type:'application/json'}" object. I saw other examples of sending blob via post and did not see this additional object included.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
user1740058
  • 45
  • 1
  • 9
  • 1
    Ok, so that makes a formData object but it does not send it to the PHP? – RiggsFolly Jan 05 '22 at 20:57
  • Didn't want to include all of the extyra code. I was previously only sending json data, but now am sending data as blob file. I know the data was sent successfully I am just unable to access the data once it has been received. – user1740058 Jan 05 '22 at 21:10
  • Just curious, is there a particular benefit to sending JSON string data as a Blob? – Markus AO Jan 05 '22 at 21:10
  • I am uploading a maximum of 2 base64 strings to a php script to be processed. This is not for multiple users, but for a single user. There seems to be an issue with sending the data as base64 string when the file is larger than 1mb on chrome and edge. Not interested in chunking, or converting string to image before uploading. – user1740058 Jan 05 '22 at 21:20
  • The whole point of the question was about how to do this with "blob" file. I was already using that method, as I stated in the question, but when working with "blob" file those methods did not work. Which is why I did word my question differently. If you do a word search on the link you directed my to, I'm sure you will see nowhere is "BLOB" mentioned. – user1740058 Jan 05 '22 at 23:22
  • Is this Q&A any help https://stackoverflow.com/questions/17657184 – RiggsFolly Jan 06 '22 at 10:15
  • Unfortunately not Riggs. It seems you can find a hundred articles telling you how to send the data via post, but not a single article on how to unpack the data to retrieve actual text on the php side. $_FILES method gets the blob, but no luck in decoding the json data within. I have tried - $BLOBdecode = json_decode($_FILES['jsonData']) - Also tried _$POST. Have had a little more luck in appending blob seperately to formdata. I can return the original blob in my response.json() , but still unable to decode the blob and access the data on the php side. – user1740058 Jan 06 '22 at 20:08
  • To anyone who runs into this same issue, answer is posted here -- https://stackoverflow.com/questions/65375833/how-to-receive-fetch-post-in-php -- Funny how many questions are shut down and directed to non effective answeres. Strange how at least 25percent of answers are found in comments after questions have already been shut down. Strange indeed! – user1740058 Jan 07 '22 at 00:04

0 Answers0