1

I'm developing an application with a SPA as frontend and a json rest API as backend. There's a rest endpoint for creating an entity with attributes and a binary file, for example a research papaer with several properties and a pdf file. The content of the file would just be a text property with the binary data encoded in base64.

I'd like the user to be able to complete the information and select the file an html input file, then create the json call with the contents of pdf file encoded in base64, all this done from the client.

The idea would be to issue an http post like the following from the client, encoding a file that the user selected from the browser:

POST /api/paper HTTP/1.1
Host: my_web_service.com
Content-Type:application/json
Accept:application/json 
body:
{
  "title": "paper title",
  "author": "author name",
  "pdf_content": "ZkxhQwAAACIQABAAAAUJABtAA+gA8AB+W8FZndQvQAyjv..."
}

Is it possible? Some js code that does something like that would be much appreciated.

Note: I'm not asking how to encode it in base64, but how to work with the binary data from the client. I found this article about the HTML5 File API. From this article I understand that using FileReader.readAsDataURL(Blob|File) I can get the base64 enconded contents of the file chosen with thw html input.

opensas
  • 60,462
  • 79
  • 252
  • 386
  • Of course it is possible. I have little experience with NodeJS backend programming, but in the API's for SPA usage I've made with Ruby this was always the way to go. – ichigolas Sep 28 '17 at 05:12
  • 1
    have you heard of `XMLHttpRequest`? – Jaromanda X Sep 28 '17 at 05:15
  • I edited the question to make it clear that I need to handle any type of file, not just images. Besides I need the user to be able to upload the file from it's desktop, not from a url. – opensas Sep 28 '17 at 05:23
  • Did you read part #3 from [this answer](https://stackoverflow.com/a/20285053/283366) ~ **3. Approach: Images from the local file system** – Phil Sep 28 '17 at 05:48
  • Oops, I just read the accepted answer and missed the most voted! Sorry, thanks a lot for the links... – opensas Sep 28 '17 at 06:03

0 Answers0