Salesforce - 是否可以将图像文件从 ContentVersion 显示到自定义 Visualforce 页面?
我编写了一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不认为目前可以从内容中提供它 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.
这应该有效:
This should work:
根据我的经验,“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.
示例:
了解更多信息,
访问在 Visual Force 页面中显示图像< /a>
Example :
<apex:image URL="/servlet/servlet.FileDownload?file={!recordId}"/>
for more information,
visit Displaying Images in a Visual Force Page
尝试
Try
<apex:image URL="/servlet/servlet.FileDownload?file={!recordId}"/>