I'm developing an file (attachments/contents) upload app and the process of my upload is that I upload file in chunks to ensure I'm not hitting view state.
For first chunk
insert new attachment
insert new Attachment att = new Attachment(ParentId = parentId,Body = EncodingUtil.Base64Decode(base64BlobValue),Name = fileName, ContentType = contentType);
For remaining chunks
update above uploaded new attachment ie Take the body of the current attachment, convert to base64 string, append base64 value sent from page, then convert back to binary for the body
for(Attachment atm : [select Id, Body from Attachment where Id = :attachmentId]){
update new Attachment(Id = attachmentId, Body = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(atm.Body) + base64BlobValue));
}
This is working great for Attachments, but when I apply same mechanism for CONTENT (ie ContentVersion) its not working as expected. ContentVersion file body (versiondata) always stores last chunk data only ie its not *APPENDING.*
For First chunk
insert new ContentVersion(
versionData = EncodingUtil.Base64Decode(base64BlobValue),
Title = fileName,
PathOnClient = '/'+fileName,
FirstPublishLocationId = libraryId);
For rest chunks
for(ContentVersion atm : [select Id, VersionData from contentversion where Id = :contentId]){
update new ContentVersion(
Id = contentId,
VersionData = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(atm.versionData) + base64BlobValue));
}
Possible Issue : Body field of Attachment is Creatable and Updatable, whereas VersionData field of ContentVersion is Creatable and Nillable (and NOT Updatable).
Any clue or suggestion, please!
Attribution to: Chirag Mehta
Possible Suggestion/Solution #1
As you mention ContentVersion is not updatable (versions are explicitly immutable, a feature of the content product), you would have to stage the blob somewhere else (perhaps an attachment) until its complete, then create the contentVersion record.
Attribution to: superfell
Possible Suggestion/Solution #2
I think that your right, there is no way to update
the ContentVersion.VersionData
field.
However, there is a versioning system, where you might be able to sequentially use the ContentDocument
to create new versions of the document each with the additional data added to the end.
You can also create a ContentVersion record and have the same ContentDocumentId so that it becomes different versions of same content and the version history will be automatically maintained.
Attribution to: jordan.baucke
Possible Suggestion/Solution #3
Yes contentversion's versiondata is not updateable. However It maintains a versionhistory object called contentversionhistory.
Attribution to: Sathya
Possible Suggestion/Solution #4
You can use Salesforce SOAP-based API to upload files into a Salesforce CRM Content. This link provides a sample Java application that uses that API:
Attribution to: Hooman
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/733