将文件(图像/音频/视频)以块的形式上传到FeedItem

时间:2015-07-23 19:13:08

标签: salesforce apex

​public static String upload(String folderId, String documentId, String fileName, String base64BlobValue) {
        if(documentId == '' || documentId == null) {
            Document document = new Document(Name=fileName, FolderId=folderId, Body=EncodingUtil.Base64Decode(base64BlobValue), IsPublic=true);
            insert document;

            return document.Id;

        } else {

            Document document = [select Id, Body from Document where Id = :documentId];
            update new Document(Id = documentId, Body = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(document.Body) + base64BlobValue));

            return documentId;
        }
    }

我试图以块的形式将文件(图像/音频/视频)上传到FeedItem,上面的示例显示了我在写入文档时如何做同样的事情。但是,如果尝试对FeedItem做同样的事情,它会说" FeedItem.ContentData不可写"所以我尝试了以下代码:

FeedItem imageFeedItem = [select Id, ContentData, RelatedRecordId from FeedItem where Id = :imageFeedItemId];

ContentVersion content = new ContentVersion();
content.versionData = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(imageFeedItem.ContentData) + base64BlobValue);
content.pathOnClient = fileName;

content.ContentDocumentId = [Select ContentDocumentId from ContentVersion where id=:imageFeedItem.RelatedRecordId].ContentDocumentId;

insert content;

但这会创建不完整块的ContentVersions。关于如何实现这一点更加整洁的任何指示。

由于

1 个答案:

答案 0 :(得分:1)

您可以尝试将IsMajorVersion设置为false

ContentVersion content = new ContentVersion();
content.versionData = EncodingUtil.Base64Decode(EncodingUtil.Base64Encode(imageFeedItem.ContentData) + base64BlobValue);
content.pathOnClient = fileName;
content.IsMajorVersion = false;

content.ContentDocumentId = [Select ContentDocumentId from ContentVersion where id=:imageFeedItem.RelatedRecordId].ContentDocumentId;

insert content;

希望有帮助!