0

I want to utilise 10MB file limit (happy to prevent 10MB files), and the standard apex:inputFile notation. Do not want to use JS API

When I use the below Apex/VF/JS I am allowed to browse and select files larger than 10MB. Which of course fail the upload process, meaning a long delay before the UI is given back to the user.

Effectively I want a warning once the file is selected if it is greater than 10MB. Or even better preventing the file from being selected in the first place. I don't mind pushing upload and then Salesforce Apex returning an error, but the time waiting for the VF to return is off the chart.

Maybe my JS can determine the File Size/blob size, and update a binding to the controller.

There are reference to FileSize in the inputFile annotation but I have not used these effectively???

Here is my Apex:

public PageReference upload() {
    if(fileSize <= 10000000){
        ContentVersion v = new ContentVersion();
        v.versionData = file;
        v.title = title;
        v.pathOnClient = (path==''?'filenamemissing':path)+(ext==''?'.jpg':ext);
        insert v;
        v = [SELECT Id, ContentDocumentId FROM ContentVersion Where Id = :v.Id];
        ContentDocumentLink theLink = new ContentDocumentLink();
        theLink.LinkedEntityId = record.Id;
        theLink.ContentDocumentId = v.ContentDocumentId;
        insert theLink;
        title='';
        path='';
        ext='';
        return myUploadPage();
    }else {
        pageMessage = 'File is Too Large: '+fileSize+'Bytes, 10MB Max';
        title='';
        path='';
        ext='';
        return null;
    }
}

Here is my VF:

<apex:inputFile value="{!file}" filename="{!fileName}" filesize="{!fileSize}" contentType="{!contentType}" onchange="updatefilename(this)" />
<apex:inputText id="title" value="{!title}" />
<apex:inputText id="path" value="{!path}" />

Here is my JS:

function updatefilename(obj_inpf) {
  var pathname = obj_inpf.value;
  var dot = pathname.lastIndexOf(".");
  var fileext = pathname.lastIndexOf("\\");
  var filenameonly = pathname.slice(fileext+1, dot);
  var ext = pathname.slice(dot);
document.getElementById('page:form:title').value = filenameonly;
document.getElementById('page:form:path').value = filenameonly;
document.getElementById('page:form:ext').value = ext;
}
1

Here are my changes to the JS to make this work. Apex is irrellevant here, if I am touching the Controller (submitting) then the file potentially has already made it to the server, whcih is the thing I am trying to avoid. If I want the value in my controller, I would need to send it to the controller outside of the Submit.

Here is my new JS

function updatefilename(obj_inpf) {
//alert('_______obj_inpf_________' + obj_inpf.value);
var input, file;
input = document.getElementById('page:form:fileinput');
file = input.files[0];
var size = file.size;
if(size<10485760){
    var pathname = obj_inpf.value;
    var dot = pathname.lastIndexOf(".");
    var fileext = pathname.lastIndexOf("\\");
    var filenameonly = pathname.slice(fileext+1, dot);
    var ext = pathname.slice(dot);

    document.getElementById('page:form:title').value = filenameonly;
    document.getElementById('page:form:path').value = filenameonly;
    document.getElementById('page:form:ext').value = ext;
    document.getElementById('page:form:sizecomment').innerHTML = 'Your file size is OK: ' + size + 'Bytes, <10MB';
}else{
    document.getElementById('page:form:fileinput').value = null;
    document.getElementById('page:form:title').value = null;
    document.getElementById('page:form:path').value = null;
    document.getElementById('page:form:ext').value = null;
    document.getElementById('page:form:sizecomment').innerHTML = 'Your file is Too Large: ' + size + 'Bytes, 10MB Max!';
    //document.getElementById('page:form:size').value = 'Your file is Too Large: ' + size + 'Bytes, 10MB Max!';
}
//alert(pathname+' '+dot+' '+slash+' '+file+' '+ext);

}

And my VF change:

<apex:inputFile id="fileinput" value="{!file}" onchange="updatefilename(this)" />

My Source: https://stackoverflow.com/a/3717847/7012749

| improve this answer | |

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.