I have created an Apex REST service that accepts a POST with a binary attachment. When the Content-Type is set to "application/pdf", it works fine, accepts the file, and the attachment is valid as an attachment in Salesforce. However, if the Content-Type is "application/vnd.ms-excel" or "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" the file becomes corrupted when stored in Salesforce. Has anyone made this work?
This is the code:
@RestResource(urlMapping='/FileTOOLS/v1.0/*')
global class FileTool {
@HttpPost
global static String attachBinary(){
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String responseSTR;
String contenttype = req.headers.get('Content-Type');
Id reqid = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
String attachName='';
if(req.headers.get('filename') != null ){
String sourcefile=req.headers.get('filename');
Integer last = sourcefile.lastIndexOf('.');
system.debug(last);
attachName = sourcefile.subString(0,last).replaceAll('\\.','_')+sourcefile.subString(last);
system.debug(attachName);
}else{
attachName='generic.txt';
}
Blob b = req.requestBody;
Integer docsize = b.size();
try{
sObject a;
if(docsize < 26214400){
a = new Attachment (ParentId = reqid,
Body = b,
ContentType = contenttype,
Name = attachName);
}else{
a = new FeedItem(parentId = reqid,
ContentData = b,
ContentFileName = attachName);
}
try{
insert a;
responseSTR= a.id ;
}catch(DMLException d){
responseSTR= d.getDMLMessage(0);
}
}catch(Exception e){
responseSTR ='Error:: - ' + e.getMessage();
}
return responseSTR;
}
application/vnd.ms-excel
and then stored the attachment using the same logic you have in your REST method? Or, instead of storing as an attachment, have the REST method also save aContentVersion
to try and help isolate the issue.