Uploading attachment using JavaScript

Answered

4 Comments

  • Sushant Awalekar
    Community Moderator

    Hi james,

    Can you try this?

    Replace Content-Type

    'Content-Type': 'image/png',

    with

    'Content-Type': 'application/binary',


    Hope this helps!!

     

    -sushant

    0
  • James Smyth

    Hello Sushant, 

    Thank you for your response but this still does not work. The image after uploading is still corrupt and can not be viewed.

    Do you have a working example?

    0
  • Sushant Awalekar
    Community Moderator

    James Smyth

    I haven't tried with Axios yet. but I have used attachment API in python scripts and it works as expected. you can give try with the postman client and check if that works for you and then you can troubleshoot the issue whether Axios is causing this or any other factors.

     

    -sushant

    0
  • FXDiamond

    Hi James Smyth

    I'm using React with axios myself and I had the same issue which is now being resolved with the following:

    First, the Content-Type must be binary, as Sushant suggested, only for the Upload API call. The Request API it will be the usual application/json:

    'Content-Type': 'application/binary',

    Second, I don't think you've converted your uploaded file to base64. You must convert before attaching it to your axios body. This way it won't break.  

    I have found this code snippet which works like a charm: https://codesandbox.io/s/convert-file-to-base64-in-react-lqi1e

    It's written for React, but you should be able to extract a JS version of it.

    0

Please sign in to leave a comment.

Powered by Zendesk