具有动态文件扩展名的SSRS外部图像

时间:2017-07-25 15:16:50

标签: image reporting-services ssrs-2008

我有一个报告,需要显示服务器上Windows文件夹中的图像,该文件正常工作(请参阅here)。现在,我想知道如何让报告提取不同文件类型的图像,如jpg& TIF。 (我默认使用png)。有一个相对简单的方法来做到这一点?带有文件扩展名的图像名称不在SQL数据库中。

编辑:我在自定义代码块中输入了这个,来自Daniel的帮助。

Public Function GetImage(ByRef Filename As String) As String ' Full image path used for testing if it exists Dim ImagePath As String ImagePath = "\\GVSSERVER1\GVSServerD\Acclamare_Images\" + Filename ' Test to see if the file exists as a gif Try If System.IO.File.Exists(ImagePath + ".png") Return "file://" + ImagePath + ".png" ElseIf System.IO.File.Exists(ImagePath + ".jpg") Else Return "file://" + ImagePath + ".jpg" End If Catch ex As Exception Return "Hit an Error" End Try Return "Hit the end" End Function
当我运行报告时,即使图像文件是png,它也会获取.jpg扩展名,并且该项目没有jpg文件。关于如何纠正的任何想法?
                                                                                                                                                                 编辑2:我没有使用更新的自定义代码取得成功,但我可能错过了一些东西,因为我不是自定义代码的专家。我发现这个问题(see here)是一个函数。我试过它并且它有效,除了某些原因.tif文件没有显示在报告上。我安装了Microsoft Picture Manager(来自Sharepoint exe下载),但它仍然没有显示.tif文件。

1 个答案:

答案 0 :(得分:3)

好消息是, definetely 可能,但需要一些自定义代码和服务器端调整才能做好准备。

一般想法:创建一个代码隐藏的函数,它接受我们的图像名称,然后执行文件存在测试以确定网络共享上实际存在哪种文件扩展名类型。

如果右键单击报告区域外部并转到属性,您将看到自定义代码窗口,您可以在其中粘贴以下功能代码。

自定义代码:

Public Function GetImage(ByRef Filename As String) As String
    ' Full image path used for testing if the image exists
    Dim ImagePath As String
    ImagePath = "\\EM-SSRS\ImageTest\" + Filename

    ' Test to see if the file exists as a gif
    If System.IO.File.Exists(ImagePath + ".gif")  THEN
            Return "file://" + ImagePath + ".gif"
    ElseIf System.IO.File.Exists(ImagePath + ".png")  THEN
            Return "file://" + ImagePath + ".png"
    ElseIf System.IO.File.Exists(ImagePath + ".jpg")  THEN
            Return "file://" + ImagePath + ".jpg"
    ElseIf System.IO.File.Exists(ImagePath + ".jpeg")  THEN
            Return "file://" + ImagePath + ".jpeg"
    End If

    Return "No Image Exists"
End Function

您必须编辑ImagePath变量以包含场景的网络共享路径,或者甚至在函数中添加另一个参数以使其更通用。

创建代码函数后,我建议在报表上创建一个虚拟文本框表达式并使用以下值:

=Code.GetImage("Filenmame")

这将允许您查看函数的输出并根据需要调整内容。请注意,“file:// ...”语法可能无法在报表生成器或visual studio中使用,并且可能必须部署到报表服务器以进行测试。

当然,一旦看起来该功能正常工作,添加图片,确保将源设置为外部并使用与文本框相同的表达式。

服务器端更改

在我自己的环境中进行进一步测试后,我必须进行两项额外的更改才能使其正常工作:

  1. Ensure that the unattended execution account is set到拥有文件共享权限的域帐户

  2. rssrvpolicy.config文件中的
  3. Edit the SSRS config信任自定义代码并允许通过指定“FullTrust”执行File.Exists函数

  4. <CodeGroup
      class="UnionCodeGroup"
      version="1'
      PermissionSetName="FullTrust"
      Name="Report_Expressions_Default_Permissions"
      Description="This code group grants default permissions for code in report expressions and code element."
      ...
    </CodeGroup>
    

    我做了这些更改后确实重启了SSRS服务(我认为这是必需的,但是作为预防措施)

    请注意,我不是SSRS服务器管理员,其他人可能能够添加有关自定义策略更改的一些其他信息。浏览MS文档,建议使用自定义程序集,以便使用这一部分。