function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hanuma KollaHanuma Kolla 

Hi all I am doubt in Uploading larse CSV file(2gb) by using only apex class and VF page. I am able to upload the file but the file is not getting processed from the custom object. Can anyone please help in this by providing some sample code.

Best Answer chosen by Hanuma Kolla
Ragava reddyRagava reddy
Hi,

Please go through the below code, I think it will helps for you

public with sharing class AttachmentUploadCon {

    public String parentId { get; set; }

  public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }

  public PageReference upload() {

    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = '008521547896510'; // the record the file is attached to
    attachment.IsPrivate = true;

    try {
      insert attachment;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      attachment = new Attachment(); 
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
  }

}

<apex:page controller="AttachmentUploadController">  
  <apex:sectionHeader title="Visualforce Example" subtitle="Attachment Upload Example"/>

  <apex:form enctype="multipart/form-data">
    <apex:pageMessages />
    <apex:pageBlock title="Upload a Attachment">

      <apex:pageBlockButtons >
        <apex:commandButton action="{!upload}" value="Save"/>
      </apex:pageBlockButtons>

      <apex:pageBlockSection showHeader="false" columns="2" id="block1">

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!attachment.name}" id="fileName"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection>

    </apex:pageBlock>
  </apex:form>
</apex:page>

Thanks,
Raghavendra Reddy.D
 

All Answers

Ragava reddyRagava reddy
Hi,

Please go through the below code, I think it will helps for you

public with sharing class AttachmentUploadCon {

    public String parentId { get; set; }

  public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }

  public PageReference upload() {

    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = '008521547896510'; // the record the file is attached to
    attachment.IsPrivate = true;

    try {
      insert attachment;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      attachment = new Attachment(); 
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
  }

}

<apex:page controller="AttachmentUploadController">  
  <apex:sectionHeader title="Visualforce Example" subtitle="Attachment Upload Example"/>

  <apex:form enctype="multipart/form-data">
    <apex:pageMessages />
    <apex:pageBlock title="Upload a Attachment">

      <apex:pageBlockButtons >
        <apex:commandButton action="{!upload}" value="Save"/>
      </apex:pageBlockButtons>

      <apex:pageBlockSection showHeader="false" columns="2" id="block1">

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!attachment.name}" id="fileName"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection>

    </apex:pageBlock>
  </apex:form>
</apex:page>

Thanks,
Raghavendra Reddy.D
 
This was selected as the best answer
Hanuma KollaHanuma Kolla
Hi Raghavendra
I tried this scenario, but getting the error as

Invalid id: 008521547896510
Error is in expression '{!upload}' in component <apex:commandButton> in page attachmentuploadvf: Class.AttachmentUploadCon.upload: line 17, column 1
An unexpected error has occurred. Your development organization has been notified.
can u please suggest me further and also need help to parse this csv file.

Thanks, 
Hanuma
Ragava reddyRagava reddy
Hi Hanuma,

You have to add your record id, which record you have to attach attachemnts.

Thanks,
Raghavendra Reddy.D
Hanuma KollaHanuma Kolla
Hi Raghavendra,

It worked out thanks a ton, but i could not process the file, if I am trying to process the file by parsing the file getting the issues like "Regex too complex" or "Heap size too large". How could I solve this?


Thanks, 
Hanuma.