如何使用ConnectApi.UserProfiles.setPhoto以base64格式为社区用户上传个人资料照片

时间:2019-02-07 13:50:32

标签: upload photo salesforce-communities

1正在使用ConnectApi.UserProfiles.setPhoto方法以base64格式为社区用户上传个人资料照片。但是出现“ ConnectApi.ConnectApiException:您上传的文件似乎不是有效的图像”的错误,请帮助我解决此问题。

2 个答案:

答案 0 :(得分:0)

嗨,您可以尝试以下方法:

public PageReference upload() {
    Blob b;
    document.AuthorId = UserInfo.getUserId();
    document.FolderId = UserInfo.getUserId(); // put it in running user's folder
    try {
        document.type = 'jpg';
        document.IsPublic = true;
        insert document;
        // ImageId = '06990000001HnuB';
        b = document.Body;
        //ConnectApi.ChatterUsers newPhoto = new ConnectApi.ChatterUsers();
    } catch (DMLException e) {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error uploading file'));
        return null;
    } finally {
        document.body = null; // clears the viewstate
        document = new Document();
    }
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'File uploaded successfully : ' + b));
    String communityId = null;
    String userId = UserInfo.getUserId();
    //ID fileId = ImageId;
    // Set photo
    ConnectApi.Photo photo = ConnectApi.ChatterUsers.setPhoto(communityId, userId, new ConnectApi.BinaryInput(b, 'image/jpg', 'userImage.jpg'));
    return null;
}

答案 1 :(得分:0)

我遇到了同样的错误,没有太多细节,所以我不太确定您的问题。我使用下面的代码解决了这个问题。根据需要进行修改。

public static Boolean updateUserProfilePic(String userProfilePicString, String userId, String fileType){
Boolean updateSuccessful = true;

System.debug('-------------' + userProfilePicString.length());
try{
    Blob blobImage = EncodingUtil.base64Decode(userProfilePicString);

    ConnectApi.BinaryInput fileUpload = new ConnectApi.BinaryInput(blobImage, 'image/jpg', 'userImage.jpg');
    ConnectApi.Photo photoProfile = ConnectApi.UserProfiles.setPhoto(null, userId,  fileUpload);
}
catch(Exception exc){
    updateSuccessful = false;
}

return updateSuccessful;

}