0

I've got a CSV file stored on the user's hard drive and I want to store it in Salesforce. The code I have uses Visualforce and Apex to, I think, open up the file and place the contents on a hidden field on the VF page, then it calls the Apex code to grab that and pass it as a string to the code listed below.

I would really like to just get the name of the file from the user and call some method to create the ContentVersion record - I would like to get Visualforce out of the loop basically. Any Apex samples to show how to do this would be appreciated.

private void attachUploadFileToFirstAccount(String filename, String firstAccount, String blobString) {
    System.debug('==> blobString: ' + blobString);
    ContentVersion cv = new ContentVersion(
            Title = filename.substringBefore('.'),
            Description = 'Uploaded on ' + Date.today(),
            FirstPublishLocationId = firstAccount,
            PathOnClient = filename,
            VersionData = EncodingUtil.Base64Decode(blobString),
            ContentLocation = 'S',
            Origin = 'H'
    );
    insert cv;
    System.debug('==> ContentVersion: [' + cv.Id + ']' + cv + '\n');
}
  • 2
    The method you shared here does insert a ContentVersion given base64-encoded contents in blobString. You can't access the user's hard drive in Apex, though - you have to have a Visualforce or Lightning front-end to allow them to upload a file. – David Reed Jan 17 at 17:32
  • 1
    WIthout VF there is no UI, No UI means no SF – Pranay Jaiswal Jan 17 at 17:39
  • 1
    Dean, if all you have is a fieldname, you're stuck. As David says, you can't access the user's hard drive. The most important question is, why do you want to get rid of the UI? – Sebastian Kessel Jan 17 at 17:45
  • Ok, this is good information. Didn't know about not being able to access the user's hard drive - I'm really poor at Apex and even poorer with VF. The reason is that as part of a bigger project we have a PDF file with lots of pages. I was thinking about trying to split up that PDF into individual files, one page per file, then getting a directory list and loading them into SF. Think of them as individual invoice pages or something to be stored on the contact record. – Dean Wooldridge Jan 17 at 18:07
  • maybe best bet is to back up and look at the generation of the PDF as we do that in SFDC but that is a separate question. – Dean Wooldridge Jan 17 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.