0

I have a process that allows users to add an attachment to an Activity object via Salesforce1 but since the contentType is not defined the file uploads but is Null. So my question is where do I reference the contentType? I tried to reference it in my Class but no luck. Also, Do I have to look for all the different file types that a user may upload? Any guidance is greatly appreciated. Cheers, M

<apex:page standardController="Task" extensions="Salesforce1FileUpload">
<apex:form enctype="multipart/form-data">
<apex:pageMessages />
<apex:pageBlock title="Upload your Attachment">
<apex:pageBlockSection showHeader="false" columns="2" id="block1">
<apex:inputFile style="width:100%" id="fileToUpload" value="{!fileBody}" />
         <apex:pageBlockSectionItem >
         <apex:outputLabel value="File Name" for="fileName"/>
         <apex:inputText value="{!filename}" id="fileName"/>
         </apex:pageBlockSectionItem>         
         </apex:pageBlockSection>   
     <br/>
        <apex:commandButton value="Save Your Attachment" action="{!UploadFile}"/>
</apex:pageBlock>

public class Salesforce1FileUpload { public Id recId { get;set; }

public Salesforce1FileUpload(ApexPages.StandardController ctlr)
{
   recId = ctlr.getRecord().Id;     
}

public string fileName 
{    get;set;    }

public string contenttype
{    get;set;    }

public Blob fileBody 
{    get;set;    }

public PageReference UploadFile()
{
    PageReference pr;
    if(fileBody != null && fileName != null)
    {
      Attachment myAttachment  = new Attachment();
      myAttachment.Body = fileBody;
      myAttachment.ContentType= contenttype;
      myAttachment.Name = fileName;
      myAttachment.ParentId = recId;
      insert myAttachment;
      pr = new PageReference('/' + myAttachment.Id);
      pr.setRedirect(true);
      return pr;
    }

    return null;
}    

}

  • Remove the contentType and then try – Tushar Sharma Aug 20 '16 at 20:24
0

Try changing your inputfile to <apex:inputFile style="width:100%" id="fileToUpload" value="{!fileBody}" contentType="{!contentType}" />. I'm basing this off of the description of the attribute on the documentation page. If you want, it looks like you can also store the filename with a filename attribute rather than taking it separately.

| improve this answer | |
  • Thank you both for your replies. IllisiveBrian. The additional syntax contentType="{!contentType}" / corrected the issue. Kinda. If I upload a file in Salesforce1 I can view the attachment, but only once. If I go back to view the attachment it only shows the name of the file and the file size. I can also view it on the desktop without any issue. But it is odd that I can only view the attachment 1 time..... – Michael Aug 22 '16 at 17:22
  • Only 1 time in Salesforce1 – Michael Aug 22 '16 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.