Recent searches


No recent searches

Uploading attachment using JavaScript

Answered


Posted Mar 17, 2021

I am trying to upload an attachment (dummy.png), the response is that the attachment has been created as I get a 201 response with a token.
 
I then attach the token to the uploads array before submitting a new ticket. The issue I am having is when I view the ticket, the attachment is corrupted. I am wondering whether it is how i am uploading the attachment in the first place.
 
This is my header below for uploading an attachment. Can someone let me know if there is an issue here?
 
Thank you.
 
const response = await axios.post(`uploads.json?filename=dummy.png`, body, {

baseURL: '/api/v2',

headers: {

common: {

Accept: 'application/json',

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

mode: 'no-cors',

Authorization: `Basic ${process.env.token}`,

},

},

withCredentials: true,

});

0

5

5 comments

Hi james,

Can you try this?

Replace Content-Type

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

with

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


Hope this helps!!

 

-sushant

0


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



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


Hi @...

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


The solution that worked for me is:

1. Converting the image/video/file to a buffer first

2. Uploading the buffer as application/binary

The code is a bit big, but not hard to understand:

user selects file -> handleFileSelectAndUpload -> readFileAsBuffer -> handleImageUpload

const readFileAsBuffer = (file) => {
const reader = newFileReader();
reader.onload = (event) => {
const buffer = event.target.result;
handleImageUpload(file.name, buffer);
};
reader.readAsArrayBuffer(file);
};

const handleImageUpload = (name, buffer) => {
fetch(
`https://<your_domain>.zendesk.com/api/v2/uploads.json?filename=${name}`,
{ method: "POST", body: buffer, headers: { ["Content-Type"]: "application/binary" } }
)
.then(response => {
if(response.ok) console.log("Success uploading image!")
else console.log("Failed during image upload")
})
};

const handleFileSelectAndUpload = (event) => {
event.preventDefault();
// This is for multiple files, but if you only need one
// use readFileAsBuffer(event.target.files[0]) instead
event.target.files.forEach(file => readFileAsBuffer(file));
}

// REACT
<input type="file" onChange={handleFileSelectAndUpload} />

// JAVASCRIPT / TYPESCRIPT
<input type="file" onchange={handleFileSelectAndUpload} />
I spent weeks trying to figure out this, and since it worked I came back to answer this post which helped me go in the right direction and maybe help others :)
 

1


Please sign in to leave a comment.

Didn't find what you're looking for?

New post