+ Start a Discussion
Pranav ChitransPranav Chitrans 

Restrict validation for file size and file type while up,loading profile picture

While uploading profile picture via logged in community I want to put a validation check file size should me less than 2 mb ans type should be of JPG/JPEG and so on..I tried but not geeting through it
File to add
<apex:inputFile value="{!blbImageBody }"   id="i_file"/>
Button
<apex:commandButton value="Save" styleClass="btn-success" style="margin-right: 5px;" 
                          html-data-loading-text="Saving..." action="{!upload}"  id="i_submit" 
                          onclick="$(this).button('loading')"/>
In this line i have to perform one more jquery method that checks the validation.Kindly help me over this issue ASAP.
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Pranav,
There is a attribute calles "filesize", you can use this attribute to restrict the filesize.
For more information -
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_inputFile.htm

Thanks,
Sumit Kumar Singh
Pranav ChitransPranav Chitrans
Hello Sumit,

And what about the type of image that it should only accept the extension like jpg/jpeg,jpig and so on
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Pranav,
There is one more attribute called "accept". Here you can sprcify the extensions seperated by comma.
Pranav ChitransPranav Chitrans

Hi Sumit,

I already  used all that..But that does not work after saving the data it shows error...I have shared the part of code for that..Kindly addon on that and suggest me.
<table Style="width: 100%;">
            <tr>
                <td>
                    <apex:inputFile value="{!blbImageBody }" fileSize="5"  id="fileinput"/>
                </td>
                <td style="text-align:right;">
                    <apex:commandButton value="Save" styleClass="btn-success" style="margin-right: 5px;" 
                          html-data-loading-text="Saving..."   id="btnLoad" 
                          onclick="$(this).button('loading');chkImg(); return false; "/>
                    <apex:commandButton value="Cancel" styleClass="btn-warning" html-data-dismiss="modal" />
                </td>
            </tr>
        </table>
 
<apex:actionFunction name="chkImgType" action="{!upload}"/>
 
function chkImg()
  {
  
   if ($("[id$='fileinput']")[0].files.length > 0) {
    //validate file attachment is not > 500k
    var uploadFileSize = $("[id$='fileinput']")[0].files[0].size;
    if (uploadFileSize > 1024 * 1024 * .5) {
        alert('File uploads must be less than 500k in size. Your file is: '+(Math.round(uploadFileSize/1024)) + 'k');
    }
    else {
       chkImgType();
       return true;
    }
}
else {
   chkImgType();
       return true;
}
  }

I tried this.But it have issue.Now addon this... :(
Pranav ChitransPranav Chitrans
Could anyoe help me on this please