Convert multiple files to base64 javascript

Lucas
Lucas Posts: 1

Hello,
We are working on a project where we need to make an REST API call in which the body of the request has an attachments field. The attachments need to be converted to base 64. The problem is that we need to send multiple attachments through the API request (the number of files is variable), so the "File encoding format" in "Web API: Send PUT request" action wouldn't work for us. Bellow is the JSON object that we need to send in the body of the request.

We are thinking of creating a Javascript code to get the file name and convert the file to base64, and then output the final JSON object. Does anyone have an idea of how to implement this code?

Answers

  • Kalin_996882
    Kalin_996882 Posts: 74 admin

    Hi @Lucas,

    Thank you for your question. Would you be able to use the below field formula to accomplish what you need, where file1 and file2 are File type fields in your workflow?

    var content = {
      "file1": "{{file1}}",
      "file2": "{{file2}}"
    }
    result = JSON.stringify(content,null,2);
    

    In the API actions there is also an option for Base64 in the File encoding format parameter of the action. Would this work in your case? Let me know if you have any questions.

    Thank you,
    Kalin

  • Hi @Kalin,
    Can you please help us what is the right syntax if we want convert the file to Base64?

    var content = { "file1":fields["uploadfile"]}
    result = JSON.stringify(content,null,2);
    Thanks