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
SS KarthickSS Karthick 

How to validate uploaded file as image

HI folks,
           Can anyone tell me how to validate the uploaded file as image?
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:form>
  
</apex:page>

and my 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 validate here

            ConnectApi.BinaryInput b=new ConnectApi.BinaryInput(file,'image/jpeg','myimage');
            

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

I dont know how to validate the blob data type
any help would be appreciatble
Thanks in advance
Karthick
Best Answer chosen by SS Karthick
kiranmutturukiranmutturu
Yes there is an attribute you can set on inputfile with "contentType" and this will store as String property that stores the uploaded file's content type. So you can get this in Apex and validate there also.

All Answers

kiranmutturukiranmutturu
I think you should write a script ot validate the same.. Some thing I did  onchange on inputfile
 
<script type="text/JavaScript">

function TestFileType( fileName, fileTypes ) {
if (!fileName) return;

dots = fileName.split(".")

fileType = "." + dots[dots.length-1];

return (fileTypes.join(".").indexOf(fileType) != -1) ?
alert('That file is OK!') : 
alert("Please only upload files that end in types: \n\n" + (fileTypes.join(" .")) + "\n\nPlease select a new file and try again.");
}

</script>

<apex:inputfile value="{!file}" onClick="TestFileType(this.form.uploadfile.value, ['gif', 'jpg', 'png', 'jpeg']);"></apex:inputfile>
SS KarthickSS Karthick
@Kiran ,
       Can you explain how the java script funtion is work?
and Is there any other method to validate the same?


Thanks in advance
Karthick
kiranmutturukiranmutturu
when you click on upload and select the file, then onchange on inputfile will fire and then it will fire the script and validate the selected filename extesion aganist the list of extensions which are passed to the method as a second paramter. First paramter is the total file name and second file name is the extensions that we want to validate 
SS KarthickSS Karthick
@Kiren,
     Is there any other way to valdiate the same?
kiranmutturukiranmutturu
Yes there is an attribute you can set on inputfile with "contentType" and this will store as String property that stores the uploaded file's content type. So you can get this in Apex and validate there also.
This was selected as the best answer
SS KarthickSS Karthick
Your script function is not working
kiranmutturukiranmutturu
Dont know from your end and the same is working like a champ for me... Try the second option then....or let me know what that error that you are getting with the javascript
 
SS KarthickSS Karthick
Second method is working and this is what I expect