<apex:inputFile value="{!attachment.body}" fileName="{!attachment.name}" id="file" contentType="{!attachment.ContentType}" accept="image/*,application/pdf"/>

If I call this from my desktop browser, I'm only able to chose files that match the criteria of accept and the ContentType is for example set to image/jpeg. That's fine.

Called from my Nexus 5 via Salesforce1, I can select any other file type and the contentType is not correctly set. The ContentType is always set to application/octet-stream.

EDIT Controller Method:

public PageReference doAddAttachment() { 
    PageReference result;
    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = expense.Id;
    attachment.IsPrivate = true;
    if(attachment.body != null) {
        try {
            insert attachment;
            result = new PageReference('/apex/expenseViewMobile?id=' + expense.Id);
        catch (DMLException e) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'File upload failed.'));
        finally {
            attachment.body = null;
            attachment = new Attachment(); 
    else {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'You need to select a file.'));
    return result;
  • do you have controller code written ? – Mohith Shrivastava Jun 11 '14 at 15:14
  • sure, I added the controller method in my post – David Renz Jun 12 '14 at 7:37

Why not just use javascript and the ajax toolkit instead with a regular html5 input file that also has accept

note this will also allow you upload file up to 25mbs instead of 10mb with apex:inputfile

 <apex:form id="form_Upload">
 <apex:pageBlock >

  <apex:pageBlockSection columns="1">

    <apex:pageBlockSectionItem >

      <input type="file" id="myuploadbutton" accept="image/*,application/pdf"/>


       <apex:pageBlockButtons location="bottom"> 

      <input type="button" id="uploadBtn" value="Attach File"/>
      <apex:commandButton action="{!back}" value="Cancel" /> 

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
   <script src="/soap/ajax/33.0/connection.js" type="text/javascript"></script>
    <script src="/soap/ajax/33.0/apex.js" type="text/javascript"></script>

  j$ = jQuery.noConflict();

  top.location.href='Set this to you current page';


  var thefiletoupload;
  var realfilesize;
  var realfilename;

  function handleFileUpload(files,obj)
  if(files[0].name === undefined){
  alert("You did not selected a file or your browser doesn't support this functionality");
    if(files[0].name.length > 80){
   alert("Your file name must be less than 80 Letters");

sforce.connection.sessionId = '{!GETSESSIONID()}'; 
   for (var i = 0; i< files.length; i++) 
       f = files[i];  
       var reader = new FileReader();                    
                reader.file = f;                               
                reader.onerror = function(e) 
                        case e.target.error.NOT_FOUND_ERR:
                          alert('File Not Found!');  
                        case e.target.error.NOT_READABLE_ERR:
                        alert('File is not readable');
                        case e.target.error.ABORT_ERR:
                            break; // noop
                            alert('An error occurred reading this file.');

                reader.onabort = function(e) 
                     alert('File read cancelled');                   

                reader.onload = function(e) 

                            var att = new sforce.SObject("Attachment");
                            att.Name = this.file.name;
                            att.ContentType = this.file.type;
                            att.ParentId = 'Get id from page param or i use hiddden input tab with an id like j$('#sfid').val() and html {!yourobj.ID}';
        att.Body = (new sforce.Base64Binary(e.target.result)).toString();

            onSuccess : function(result, source) 
                if (result[0].getBoolean("success")) 
                        top.location.href='your page/ plus id';         
                    console.log("failed to create attachment " + result[0]);
            onFailure : function(error, source) 
                console.log("An error has occurred " + error);
j$('#myuploadbutton').bind('change', function () {
   realfilesize = this.files[0].size;
   realfilename = this.files[0].name;
   thefiletoupload = this.files;
if (thefiletoupload !== undefined && thefiletoupload !== null){
document.getElementById("uploadBtn").disabled = true;
handleFileUpload(thefiletoupload ,obj);
alert('You have not Uploaded a file');



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.