Salesforce - 是否可以将图像文件从 ContentVersion 显示到自定义 Visualforce 页面?

发布于 2024-10-27 09:53:52 字数 1760 浏览 3 评论 0原文

我编写了一个简单的 Visualforce 页面,让用户上传图像文件 将文件保存到 ContentVersion 对象。

现在我想在我的自定义 Visualforce 页面中显示保存的图像。这可能吗? 看起来 无法使用。 也没有运气。

真正的问题是我确实成功上传了图像文件,但它的 URL 是什么? 我在 google 上使用外部随机 URL 进行了测试,我可以显示图像(例如 /../..some.jpg")。但我无法弄清楚已上传到 contentversion 的图像文件的绝对 URL 是什么 。

注意:这不是静态资源,因为我的用户可能会经常上传图像来更改其用户图像

代码

public with sharing class ImageUploadTestController {

    public blob file { get; set; }
    public String imageFilePath { get; set; }

    public ContentVersion cv { get; set; }

    public ImageUploadTestController() {
        cv = [select id, versionData, title, pathOnClient FROM ContentVersion limit 1];
    }

    //fill out the inputFile field and press go. This will upload file to the server
    public PageReference go() {
        ContentVersion v = new ContentVersion();
        v.versionData = file;
        v.title = 'some title';
        v.pathOnClient ='/foo.jpeg';
        insert v;

        return new PageReference('/' + v.id);
    }

    //v.id sample
    //069A00000009Ux3

}//end class

Visualforce 页面

<apex:page controller="ImageUploadTestController">
    <apex:form >
    <apex:inputFile value="{!file}" />

    <apex:commandbutton action="{!go}" value="go"/>
    </apex:form>

    <!-- none of below works!! :( -->
    <a href="/{!cv.id}">{!cv.title} {!cv.pathOnClient}</a>
    <a href="https://na7.salesforce.com/069A00000009FG4"></a>
    <apex:image value="/069A00000009Ux3" width="220" height="55"/>

</apex:page>

I wrote one simple Visualforce page that let user upload an image file then
save the file to ContentVersion object.

Now I want to display the saved image in my custom visualforce page. Is it even possible?
Looks like <apex:image> cannot be used. Also <img href="{!cv.contentVersion}"...> had no luck.

The real problem is I did upload the image file successfully but what is the URL to it?
I tested with random URL outside on google and I can display the image (like /../..some.jpg"). But I can't figure out what is the absolute URL for the image file that has been uploaded to contentversion.

NOTE: This is not static resource as my users may upload image to change their user image often.

Code

public with sharing class ImageUploadTestController {

    public blob file { get; set; }
    public String imageFilePath { get; set; }

    public ContentVersion cv { get; set; }

    public ImageUploadTestController() {
        cv = [select id, versionData, title, pathOnClient FROM ContentVersion limit 1];
    }

    //fill out the inputFile field and press go. This will upload file to the server
    public PageReference go() {
        ContentVersion v = new ContentVersion();
        v.versionData = file;
        v.title = 'some title';
        v.pathOnClient ='/foo.jpeg';
        insert v;

        return new PageReference('/' + v.id);
    }

    //v.id sample
    //069A00000009Ux3

}//end class

Visualforce page

<apex:page controller="ImageUploadTestController">
    <apex:form >
    <apex:inputFile value="{!file}" />

    <apex:commandbutton action="{!go}" value="go"/>
    </apex:form>

    <!-- none of below works!! :( -->
    <a href="/{!cv.id}">{!cv.title} {!cv.pathOnClient}</a>
    <a href="https://na7.salesforce.com/069A00000009FG4"></a>
    <apex:image value="/069A00000009Ux3" width="220" height="55"/>

</apex:page>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

檐上三寸雪 2024-11-03 09:53:52

我不认为目前可以从内容中提供它 ScottW 提供的格式适用于文档。

我所做的另一个选择是将包含图像的 zip 文件上传到静态资源,然后可以引用该静态资源。

I don't believe its possible to serve it from content currently The format provided by ScottW works for Documents.

The other option I've done is upload a zip file containing my images to the Static Resources which can then be referenced.

壹場煙雨 2024-11-03 09:53:52

这应该有效:

public PageReference getContent(){
    String html = ApexPages.currentPage().getParameters().get('html');
    PageReference redirection = null;
    redirection = new PageReference('/sfc/servlet.shepherd/version/download/' + contentVersion.Id);
    redirection.setRedirect(true);
    return redirection;
}

This should work:

public PageReference getContent(){
    String html = ApexPages.currentPage().getParameters().get('html');
    PageReference redirection = null;
    redirection = new PageReference('/sfc/servlet.shepherd/version/download/' + contentVersion.Id);
    redirection.setRedirect(true);
    return redirection;
}
无畏 2024-11-03 09:53:52

根据我的经验,“thegogz”是正确的 - 目前无法直接从内容渲染图像。正如其他人所指出的,从文档和/或静态资源进行渲染确实有效,而且恕我直言,使用文档更可取,因为您可以在 Apex 中以编程方式访问 URL、二进制数据等。

Based on my experience, "thegogz"is correct - it is not currently possible to render images directly from Content. As others have indicated, rendering from Documents and/or Static Resources does work, and IMHO using Documents is preferable because you can access the URL, binary data, etc. programmatically in Apex.

帝王念 2024-11-03 09:53:52
  • 通过使用 URL 属性,在标记中使用“/servlet/servlet.FileDownload?file=”显示图像。

示例:

了解更多信息,
访问在 Visual Force 页面中显示图像< /a>

  • The image is shown using "/servlet/servlet.FileDownload?file=" in tag by using URL attribute.

Example : <apex:image URL="/servlet/servlet.FileDownload?file={!recordId}"/>

for more information,
visit Displaying Images in a Visual Force Page

诗笺 2024-11-03 09:53:52

尝试

Try <apex:image URL="/servlet/servlet.FileDownload?file={!recordId}"/>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文