Ticket attachment file not working after download

Answered

11 Comments

  • SERGE BERTAINADUBOIS

    Hi Luke!

    Maybe you should use the following content type to upload the file:

    'Content-type': 'application/binary'


    friendships,
    Serge.

    0
  • Luke Smith

    Thanks for the reply SERGE BERTAINADUBOIS

    Unfortunately that didn't work. :(

    0
  • SERGE BERTAINADUBOIS

    Luke,

     

    Here is all my python code snippet on this attachment part:

        

          # GENERATION TOKENS ATTACHEMENT

          directory = ticket_dir + '/' + str(TicketID) # contain all my files

            tab_token = []

            for filename in os.listdir(directory):

              headers = {'Content-type': 'application/binary'}
              r_attach = requests.post(AuthConfig.get('zendesk','url') + '/api/v2/uploads.json?filename=' + filename, data=open(ticket_dir + '/' + str(TicketID) + '/' + filename, 'rb'), headers=headers, auth=(AuthConfig.get('auth','user') , AuthConfig.get('auth','pass'))) # Upload file
              data_token = r_attach.json()
              cur_token = data_token['upload']['token']
                tab_token.append(cur_token)

          # TICKET CREATION
          url_ticket = AuthConfig.get('zendesk','url') + "/api/v2/tickets.json"
          false = 'false'
            data_ticket = {'ticket': {'subject': SUJET, 'comment': { 'html_body': COMMENTAIRE ,  'public' : false , 'uploads':  tab_token }, 'group_id' : group_id , 'assignee_id' : assignee_id , 'requester_id' : requester_id , 'ticket_form_id' : AuthConfig.get('zendesk','FORM_ID') , 'type' : 'question' , 'priority' : 'normal' }}
           

    Maybe something you find will help you.

    Friendships,
    Serge.

    0
  • Luke Smith

    I'm at a loss here. The file continues to not work. Here's my latest ajax code for creating upload/attachment:

    {
        url: "https://{obscured}.zendesk.com/api/v2/uploads?filename="+file_name,
        type: "POST",
        headers: {
            'Authorization': 'Basic '+wp_helper.zendesk_token,
            'Content-type': 'application/binary'
        },
        processData: false,
        data: file_base64
    }

    Note I'm passing the raw base64 content of the file.

    0
  • Eric Nelson
    Zendesk Developer Advocacy
    Hey Luke,

    What kind of file are you trying to upload? 
    0
  • Luke Smith

    Hey Eric Nelson,

    I'm trying to upload a PDF file, but will need ability to upload docx, png etc. as well.

    0
  • Eric Nelson
    Zendesk Developer Advocacy
    Makes sense, just to confirm you're trying to do this via client-side javascript and not node.js?
    0
  • Luke Smith

    That's right yep.. using AJAX to send POST request to Zendesk api. :)

    0
  • Eric Nelson
    Zendesk Developer Advocacy
    Here is an example using the fetch api, though the concept is the same. Make sure to set your Content-Type so that it matches the file type that you're uploading. 
     
     
     


    async function uploadFile(data) {

    const file = await fetch(data);
    const content = await file.blob();
    const request = await fetch("https://EXAMPLE.zendesk.com/api/v2/uploads.json?filename=example.pdf", {
    method: 'POST', 
    headers: {
    'Content-Type': 'application/pdf',
    'Authorization': 'Basic ABC123EXAMPLE'

    }, body: content
    });
    let response = await request.json()
    console.log(response)
    }
    1
  • Luke Smith

    Eric Nelson You rock! That worked perfectly. The thumbnails in Zendesk tickets now download a valid file that can be opened. We can close this ticket.

    Really appreciate it!

    0
  • Eric Nelson
    Zendesk Developer Advocacy
    Hey Luke, 
     
    Glad that worked for you, let us know if you need anything else.
    0

Please sign in to leave a comment.

Powered by Zendesk