You need to sign in to do that
Don't have an account?
bblay
Limiting File Size
I was curious if someone might be able to help me limit the attachment size a user is allowed to upload on my VF page to only 2MB. I found this code online and have no idea where to start. I'm thinking it would likely just be a couple lines of code to check the file size and display an error message if more than 2MB but I'm not sure how to do this.
Any ideas or help would certainly be appreciated! Thanks!
public class VFFileUpload { public Id recId {get;set;} public VFFileUpload(ApexPages.StandardController ctlr) { recId = ctlr.getRecord().Id; } public string fileName {get;set;} public Blob fileBody {get;set;} private integer fileSize; public Blob getFileBody() { return this.fileBody; } public void setFileBody(Blob fileBody) { this.fileBody = fileBody; setFileSize(this.fileBody.size()); } public Integer getFileSize() { return this.fileSize; } public void setFileSize(Integer fileSize) { this.fileSize = fileSize; } public PageReference UploadFile() { PageReference pr; if(fileBody != null && fileName != null) { Attachment myAttachment = new Attachment(); myAttachment.Body = fileBody; myAttachment.Name = fileName; myAttachment.ParentId = recId; insert myAttachment; Member__c m = [select id from Member__c WHERE ID=:System.currentPageReference().getParameters().get('id')]; List<Attachment> a = [select id from attachment where parentid = :m.id]; if(a.size() > 7){ delete a; } PageReference page = ApexPages.currentPage(); page.setRedirect(true); return page; } return null; } }
You can retrieve the blob size in characters which should give you a reasonable approximation. E.g.
All Answers
You can retrieve the blob size in characters which should give you a reasonable approximation. E.g.
This did exactly what I needed it to do! Thank you so much for your quick help! That's what I love about the Force.com boards, everyone is so friendly and willing to help out others :)