Upload image attachment via Api

Hi, How can i upload an image attachment to so this one shows preview for Asana task, now it is showing just an icon.
Thnx.

1 Like

Can you share a screenshot of what you are referring to as icon?

@Bastien_Siebman I am having the same problem. (I am using Integromat to access the Asana API.). I am able to successfully upload the image as an attachment using the API, but the only option I have for viewing it is to download the attachment, and view it manually. Conversely, if I manually upload an image to a task, Asana shows me a preview while viewing the task. Is there any way to get this functionality when attaching (uploading) an image to a task using the API?

Looks like the mime type or something is broken… I did succeed on my end, do you want to see the code?

2 Likes

Yes, please.

1 Like

My method to upload an attachment

 upload(fileAsBase64: string, taskGid: string, filename: string): Observable<Attachment> {
    const formData = new FormData()
    const file = this.base64ToFile(fileAsBase64, filename)
    formData.append('file', file)
    return this.http.post(`https://app.asana.com/api/1.0/tasks/${taskGid}/attachments`, formData,
      {
        headers: {
          'Authorization': 'Bearer ' + localStorage.getItem('access_token')
        }
      }).pipe(map(ret => new Attachment(ret)))
  }

and the code to get a file into a base64 string

/**
   * Source: the web
   */
  base64ToFile(dataurl, filename) {
    const arr = dataurl.split(',')
    const mime = arr[0].match(/:(.*?);/)[1]
    const bstr = atob(arr[1])
    let n = bstr.length
    const u8arr = new Uint8Array(n)
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n)
    }
    return new File([u8arr], filename, {type: mime})
  }
1 Like

@Bastien_Siebman Thanks. I’ll see if I can translate this to the platform I am using.

I see about 5 posts on this forum from people having the same issue with attachments. Do you have any idea what the difference is, between when the preview is working, and not (i.e.,. when you can only download the attached image to see it).?

No sorry, my intuition is that it has to do with the mime-type, the header of the file or the extension.

It was the extension! Just needed to add “.jpg” to the end of the filename. Thanks for your help!

2 Likes