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
Sai Ram ASai Ram A 

Upload different formats of files into CONTENT VERSION - Library


*********VF Page***************

<apex:page controller="uploadFile">
<apex:messages />
<apex:form enctype="multipart/form-data" >
<apex:pageBlock title="Contribute Content" >
<apex:commandbutton action="{!upload}" value="Publish" status="status"/>
<apex:pageBlockSection columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel > Title </apex:outputLabel>
<apex:inputTextarea value="{!title}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
title
<apex:inputFile value="{!file}" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

*********Apex Class************
public class uploadFile {
public blob file { get; set; }
public String title{get;set;}
public ContentVersion cv { get; set; }


public PageReference upload() {
ContentVersion cv = new ContentVersion();
cv.versionData = file;
cv.title = title;
cv.pathOnClient ='/test img.jpeg';
cv.FirstPublishLocationId = '01dfr000000000gA'; //Change id
try
{
insert cv;
system.debug('*********************Result'+cv.pathonClient);
}
catch (DMLException e)
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading Document in Library'));
return null;
}
finally
{
cv= new ContentVersion();
}

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

My question Here is i could only upload jpeg files , and can see preview in library.

What if want to upload different formats of files...like pdf, csv, doc, txt jpeg, png etc....

Please help me wit this.. I would really appreciate for Quick response

Thanks

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Sai Ram ASai Ram A

*********VF Page***************

<apex:page controller="uploadFile">
<apex:messages />
<apex:form enctype="multipart/form-data" >
<apex:pageBlock title="Contribute Content" >
<apex:commandbutton action="{!upload}" value="Publish" status="status"/>
<apex:pageBlockSection columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel > Title </apex:outputLabel>
<apex:inputTextarea value="{!title}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
title
<apex:inputFile value="{!file}" fileName="{!fN}" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

*********Apex Class************
public class uploadFile {
public String Type { get; set; }
public String fN { get; set; }
public blob file { get; set; }
public String title{get;set;}
public ContentVersion cv { get; set; }

public PageReference upload() {
ContentVersion cv = new ContentVersion();
cv.versionData = file;
cv.title = title;
//cv.versionData = EncodingUtil.Base64Decode(ConType);
cv.pathOnClient = fN;
cv.FirstPublishLocationId = '01dfr000000000gA'; //Change id
try
{
insert cv;
system.debug('*********************Result'+cv.pathonClient);
}
catch (DMLException e)
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading Document in Library'));
return null;
}
finally
{
cv= new ContentVersion();
}

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

All Answers

Sai Ram ASai Ram A

Tried this approach..

 

 

 

ContentVersion(
versionData = EncodingUtil.Base64Decode(base64BlobValue),
Title = fileName,
PathOnClient = '/'+fileName,
FirstPublishLocationId = libraryId);

 

 

http://salesforce.stackexchange.com/questions/733/isnt-contentversion-versiondata-updateable-amendable

Sai Ram ASai Ram A

*********VF Page***************

<apex:page controller="uploadFile">
<apex:messages />
<apex:form enctype="multipart/form-data" >
<apex:pageBlock title="Contribute Content" >
<apex:commandbutton action="{!upload}" value="Publish" status="status"/>
<apex:pageBlockSection columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel > Title </apex:outputLabel>
<apex:inputTextarea value="{!title}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
title
<apex:inputFile value="{!file}" fileName="{!fN}" contentType="{!Type}" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

*********Apex Class************
public class uploadFile {

public String Type { get; set; }

public String fN { get; set; }

public blob file { get; set; }
public String title{get;set;}
public ContentVersion cv { get; set; }


public PageReference upload() {
ContentVersion cv = new ContentVersion();
//cv.versionData = file;
cv.title = title;
cv.versionData = EncodingUtil.Base64Decode(ConType);

cv.pathOnClient = '/file';
cv.FirstPublishLocationId = '01dfr000000000gA'; //Change id
try
{
insert cv;
system.debug('*********************Result'+cv.pathonClient);
}
catch (DMLException e)
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading Document in Library'));
return null;
}
finally
{
cv= new ContentVersion();
}

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

 

Still Could not be able to insert all able to upload only few formats of files. still could not able to see preview in library..

 Tried this link : 

http://boards.developerforce.com/t5/Apex-Code-Development/Decode-Encode-Word-Documents-in-Apex/td-p/329545

 

Sai Ram ASai Ram A

*********VF Page***************

<apex:page controller="uploadFile">
<apex:messages />
<apex:form enctype="multipart/form-data" >
<apex:pageBlock title="Contribute Content" >
<apex:commandbutton action="{!upload}" value="Publish" status="status"/>
<apex:pageBlockSection columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel > Title </apex:outputLabel>
<apex:inputTextarea value="{!title}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
title
<apex:inputFile value="{!file}" fileName="{!fN}" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

*********Apex Class************
public class uploadFile {
public String Type { get; set; }
public String fN { get; set; }
public blob file { get; set; }
public String title{get;set;}
public ContentVersion cv { get; set; }

public PageReference upload() {
ContentVersion cv = new ContentVersion();
cv.versionData = file;
cv.title = title;
//cv.versionData = EncodingUtil.Base64Decode(ConType);
cv.pathOnClient = fN;
cv.FirstPublishLocationId = '01dfr000000000gA'; //Change id
try
{
insert cv;
system.debug('*********************Result'+cv.pathonClient);
}
catch (DMLException e)
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading Document in Library'));
return null;
}
finally
{
cv= new ContentVersion();
}

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

This was selected as the best answer
Muzammil BajariaMuzammil Bajaria
Thanx sai ram.
michael Dawson 7michael Dawson 7
How would you restrick a file format from being uploaded ContentVersion?