4

Can anyone tell me how to validate the uploaded file is image using blob data type?
My vfp:

   < apex:page standardController="Contact" sidebar="false" extensions="DpConnectApiExt">  
    < apex:form >

        <apex:outputPanel id="ProfilePicturePanel">
            <apex:pageMessages ></apex:pageMessages>
            <apex:outputText ><b>Current User Profile Picture Using Connect API</b></apex:outputText><br/>
            <apex:actionRegion >
            <center> <apex:image url="{!UserfullPhoto}" />    </center><br/><br/>
            </apex:actionRegion>
        </apex:outputPanel>
            <apex:outputtext > <b> Uploading Image Via Connet API </b></apex:outputtext>
            <apex:inputfile value="{!file}"></apex:inputfile>
            <apex:commandButton value="Submit" action="{!upload}"  />
            <apex:detail subject="{!Contact.Id}" title="false" relatedListHover="false"/>

    </apex:form>

</apex:page> 

Controller:

global class DpConnectApiExt {

    global String UserfullPhoto { get; set; }
    global transient Blob file{get;set;}
    global DpConnectApiExt(ApexPages.StandardController controller) {

        ConnectApi.Photo p = ConnectApi.ChatterUsers.getPhoto(null, UserInfo.getUserId());
        UserfullPhoto =p.fullEmailPhotoUrl;
    }
    public void upload(){
            system.debug('File:'+file);
**//I want to handle here**   
            ConnectApi.BinaryInput b=new ConnectApi.BinaryInput(file,'image/jpeg','myimage');


            ConnectApi.ChatterUsers.setPhoto(null,userinfo.getUserId(),b);   


    }
}

Thanks in advance
Karthick

1 Answer 1

6

You will need to check the Content-Type of attachment loaded

String contenttype=Attachment.contenttype;
 if(contenttype=='image/jpeg' || contenttype=='image/png' || contenttype=='image/gif' || contenttype=='image/jpg' ){

}
1
  • 4
    You could also check the first 3-8 bytes of the data for certain signatures as listed on List of file signatures.
    – sfdcfox
    Commented Dec 15, 2014 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .