0

I'm trying to upload a PDF file using jQuery. What I need to do is send a JSON in a POST body that contains the base64 of the PDF file, so that I can decode it & save it using PHP.

What I have is a html file input:

<input type="file" id="user_tc_file_path">

And what I'm trying to do is use the btoa() function as shown:

let file = btoa($('#user_tc_file_path').prop('files')[0]);

But the variable "file" containes a very short string which, if decoded, reads

[object Object]

Can someone enlighten me as to what I'm doing wrong?

Thanks!

LDR
  • 1
  • 2
  • getting `files` from the input doesn't give you the files contents, it's a [FileList](https://developer.mozilla.org/en-US/docs/Web/API/FileList) object, which contains info about the files, but not the content. To get the content, refer for example to [this answer using File API](https://stackoverflow.com/a/754398/7393478). However, I don't understand why you don't directly send the PDF file as it is. IMO it's safer than making your server accept base64 data from client, which can be anything, not only PDF encoded – Kaddath Apr 02 '19 at 12:31
  • Thanks for the input! How can I send the PDF as it is? I'm using a REST API with AJAX. – LDR Apr 02 '19 at 12:36
  • What do you mean by "I'm using a REST API"? An API can be anything. Do you mean you have no control over the Server-side? If the case, you need to follow this API specifications, if you really need to base64 encode it, read the answer using File API. If you have control over server side, just make your own script – Kaddath Apr 02 '19 at 12:51

0 Answers0