Recent searches
No recent searches
Uploaded images by API gets corrupted
Posted Mar 04, 2024
I'm trying to upload an image from an input[type=file], but when I try to open or attach it to a ticket, the image is broken.
Details :
I am using Nuxt3
function that send to backend
the sent data:

the route that receive the data:

the function to upload the image

response from /api/v2/uploads.json :
{
"attachment": {
"url": "https://territoriosaber.zendesk.com/api/v2/attachments/23461691516059.json",
"id": 23461691516059,
"file_name": "test_upload.png",
"content_url": "https://territoriosaber.zendesk.com/attachments/token/QjqqarQt44JkCnTNrcvJx8Vjt/?name=test_upload.png",
"mapped_content_url": "https://territoriosaber.zendesk.com/attachments/token/QjqqarQt44JkCnTNrcvJx8Vjt/?name=test_upload.png",
"content_type": "image/png",
"size": 102032,
"width": null,
"height": null,
"inline": false,
"deleted": false,
"malware_access_override": false,
"malware_scan_result": "malware_not_found",
"thumbnails": [
]
}
}
{
"attachment": {
"url": "https://territoriosaber.zendesk.com/api/v2/attachments/23461691516059.json",
"id": 23461691516059,
"file_name": "test_upload.png",
"content_url": "https://territoriosaber.zendesk.com/attachments/token/QjqqarQt44JkCnTNrcvJx8Vjt/?name=test_upload.png",
"mapped_content_url": "https://territoriosaber.zendesk.com/attachments/token/QjqqarQt44JkCnTNrcvJx8Vjt/?name=test_upload.png",
"content_type": "image/png",
"size": 102032,
"width": null,
"height": null,
"inline": false,
"deleted": false,
"malware_access_override": false,
"malware_scan_result": "malware_not_found",
"thumbnails": [
]
}
}

already try looking at https://developer.zendesk.com/documentation/ticketing/using-the-zendesk-api/adding-ticket-attachments-with-the-api/
but I must be missing something
If anyone could help I would be very grateful
thank you
EDIT: add the token response
thank you
EDIT: add the token response
0
9
9 comments
Lydia C
Hi,
I had similar issue with the same result: File uploaded but not with the correct format so it couldn't be opened in Support. For me worked getting the file in the following way and passing the blob to the POST call to ZD using fetch:
const localFile = await fetch(URL.createObjectURL(file))
const blob = await localFile.blob()
Regards,
Lydia
0
Suporte
Hi Lydia, sorry for the delay...

I've tryied your suggestion...
response
and the image still broken...
any other idea?
thank for replying
0
Lydia C
I think you must pass the blob of the file to the uploads endpoint and indicate that content type is binary:
0
Amir S.
Issue still occurs to me with image files.
Code below for PDF uploads works! (using Content-Type: application/binary);
PHP/laravel, curl and correct content-type used.
Any thougths?
$headers = array();
$headers[] = 'Content-Type: image/jpeg';
$attachmentUrl = 'https://storage.googleapis.com/orderaprint/orders/149872/367636_gooten_tote_bag_1711868333.jpg';
$attachmentFileData = file_get_contents($attachmentUrl);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.config('zendesk.sub_domain').'.zendesk.com/api/v2/uploads?filename=test.jpg');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERPWD, config('zendesk.username').':'.config('zendesk.token'));
$post = array('file' => $attachmentFileData);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
die();
0
Marcus Dawideit
Hello,
are there any new findings? I am facing the same problem, all files except PDF are displayed as corrupt.
Thanks
Marcus
0
Amir S.
foreach ($files as $key => $file)
{
$attachmentFile = $this->uploadAndGetPath($file);
$attachmentMime = 'application/binary';
$tmp = explode('/', $attachmentFile);
$file_name = end($tmp);
$tmp = explode('.', $attachmentFile);
$file_extension = end($tmp);
if ($file_extension=='pdf') { $attachmentMime = 'application/binary'; } // application/pdf
if ($file_extension=='zip') { $attachmentMime = 'application/zip'; }
if ($file_extension=='jpg') { $attachmentMime = 'image/jpeg'; }
if ($file_extension=='png') { $attachmentMime = 'image/png'; }
$headers = array();
$headers[] = 'Content-Type: '.$attachmentMime;
$headers[] = 'Authorization: Basic '.base64_encode(config('zendesk.username').':'.config('zendesk.token'));
$attachmentFileData = file_get_contents(Storage::url($attachmentFile));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.config('zendesk.sub_domain').'.zendesk.com/api/v2/uploads.json?filename='.$file_name);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachmentFileData);
$result = curl_exec($ch);
$result = json_decode($result,true);
curl_close($ch);
$uploadTokens[] = $result['upload']['token'];
//unlink(getcwd().'/'.$attachmentFile);
}
0
Vivek Singh
It's still happening, using the upload api, only pdf files are opening in the support after upload, any other format get corrupted, Any solution for this ?
0
Brad Masciotra
Hey Same boat as everyone here, is the API expecting a different format like base 64 for the images or binary data? PDFs work but images such as png and jpg are not going through
0
Brad Masciotra
Hey Vivek Singh I was able to solve this in C# by doing something like this
It looks like the endpoint just wants that file without anything else, I was trying to send a MultipartFormDataContent object before with no luck.
Im getting positive results with PDF, PNG and JPGs so far, so maybe try something along these lines in whatever language youre working in.
Best of luck
0