function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DJ 367DJ 367 

Files are not opening except text in external server-Rest API

Hello Everyone,
I am working on one of the requirment where I am uploading files into salesforce and sending it to external server . While I cross verify in external server , I am able to open only text fomrat other than .txt none other format is opening and (point-2) another thing is if file size is more than 5 MB it is faling to send to external server . Need help if someone has ever faced similer challage.
Aman PathakAman Pathak
hi , it depends on how  how you are setting the content-type in request and how you are sending the body.
Please share code snippets to get a better understanding of the issue.

DJ 367DJ 367
Hi Aman,

I am using ContentVersion to store files and here is my code snippets. Thanks
String contentType = '';
                if(fileExtension !=null && fileExtension.contains('txt')){
                    contentType = 'text/plain';
                }else if(fileExtension !=null && fileExtension.contains('xlsx')){
                    contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                    System.debug('contentType of ExcelSheet:'+contentType);
                }else if(fileExtension !=null && fileExtension.contains('xls')){
                    contentType = 'application/';
                    System.debug('contentType of ExcelSheet:'+contentType);
                }else if(fileExtension !=null && fileExtension.contains('docx')){
                   // contentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
                    contentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
                    //contentType = 'application/';
                    System.debug('contentType of DcoumentX:'+contentType);
                }else if(fileExtension !=null && fileExtension.contains('doc')){
                    contentType = 'application/msword';
                   // contentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template';
                    //contentType = 'application/';
                    System.debug('contentType of WorkDcoument:'+contentType);
                }else if(fileExtension !=null && fileExtension.contains('pdf')){
                    contentType = 'application/pdf';
                    System.debug('contentType of Pdf:'+contentType);
                }else if(fileExtension !=null && fileExtension.contains('zip')){
                    contentType = 'application/x-zip-compressed';
                    System.debug('contentType of Zip:'+contentType);
                }else if(fileExtension !=null && fileExtension.contains('jpg')){
                    contentType = 'image/jpeg';
                    System.debug('contentType of jpg:'+contentType);
                }else if(fileExtension !=null && fileExtension.contains('png')){
                    contentType = 'image/png';
                    System.debug('contentType of png:'+contentType);
                }else {
                    contentType = 'application/octet-stream';

DJ 367DJ 367
Hi ,
Here is the output that I am getting into external server, File is opening but in corrupt format.

User-added image