I have a report that needs to show images from a Windows folder on the server, which is working (see here ). Now, I am wondering how to get the report to pull images of differing file types, like jpg & tif. (I am using png by default). Is there a relatively easy way to do this? The image names with file extension are not in the SQL database.
EDIT: I entered this in the Custom Code block, from Daniel's help below.
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
When I run the report, it fetches the .jpg extension even though the image file is a png, and there isn't a jpg file for that item. Any idea on how to correct that?
EDIT 2: I wasn't having success with the updated custom code, but I could have been missing something, as I'm no expert with custom code. I found this question (see here) which is for a function. I tried it and it works, except for some reason .tif files don't display on the report. I installed Microsoft Picture Manager (from the Sharepoint exe download), but it still doesn't display the .tif files.
The good news is that this is definetely possible, however it takes a bit of custom code and server side tweaking to get ready.
General Idea: Create a code behind function that takes the name of our image and then does file existence tests to determine which file extension type actually exists on the network share.
If you right-click outside of the report area and go to properties, you will see the custom code window where you can paste the following function code.
Custom Code:
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
You will have to edit the ImagePath variable to contain the network share path for your scenario or even add another parameter to the function to make it more generic if you wish.
Once the code function is created, I would advise creating a dummy textbox expression on the report and using the following value:
=Code.GetImage("Filenmame")
Which will allow you to view the output of the function and tweak things as needed. Note that the "file:// ... " syntax may not work from within report builder or visual studio and may have to be deployed to a report server for testing.
Of course, once it looks like the function is working, add the image, make sure to set the source to external and use the same expression as for the textbox.
Server Side Changes
Upon further testing on my own environment, I've had to make two additional changes to get this to work:
Ensure that the unattended execution account is set to a domain account that has permission to the file share
Edit the SSRS config in the rssrvpolicy.config file to trust custom code and allow the execution of the File.Exists function by specifying "FullTrust"
<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>
I did restart the SSRS service after making these changes (I assume that's required, but did it as a precaution)
Note that I am not an SSRS server admin and someone else may be able to add some additional information about the custom policy change. Glancing through the MS documentation, there is a recommendation about using custom assemblies instead so take this part with a grain of salt.
Related
I'm creating a Classic ASP page and trying to determine if a .jpg is on my web server. If the image exists then I want to use it, but if the image does not exist, I display a generic image. This is always false. Any ideas?
<%dim fs
strFilePath =("http:/example.com/photos/" & PersonID & ".jpg")
set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FileExists(strFilePath) then
response.write "<img src='../photos/"& PersonID &".jpg'"&">"
else%>
<img src="http://exmaple.com/images/nophoto.jpg">
<%end if
set fs=nothing%>
The Scripting.FileSystemObject only supports accessing files from the file system and has no way to determine whether a file exists at a particular external URL. If the URL is within the current Web Application you can use;
Server.MapPath(relative_path)
which if passed a relative server path i.e "/photos" will return the physical path to the file on the server, which you can then test with fs.FileExists().
But if the URL is external you still have options. By using a server-side XHR request to the URL and based on the response, determine it's existence. We can also make this more efficient by only asking whether it is there and not returning the content, which we can do by using a HEAD request.
Here is an example of a possible implementation;
<%
Function CheckFileExists(url)
Dim xhr: Set xhr = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
With xhr
Call .Open("HEAD", url)
Call .Send()
CheckFileExists = (.Status = 200)
End With
End Function
If CheckFileExists("https://cdn.sstatic.net/Img/unified/sprites.svg?v=fcc0ea44ba27") Then
Call Response.Write("File Exists")
Else
Call Response.Write("File Doesn't Exist")
End If
%>
Output:
File Exists
Useful Links
Does File Exist? ASP Classic (Explains use of Server.MapPath())
In Reporting Services 2005, I need to update an embedded image file in over 300 reports to use a new image (re-branding).
I was able to come up with a script that changes the RDL to what I want, but when I run the report in Report Manager, it continues to use the old report definition and display the old image. If I download the RDL to a text file, it has my changes. When I re-upload it, the report displays the new image.
I'd like to know if I need to do something else to tell Reporting Services that there is a new definition.
My t-sql statement is below:
-- Pull updated image code from a sample report I updated with new logo
DECLARE #NewImageString VARCHAR(MAX)
SELECT #NewImageString = SUBSTRING(convert(VARCHAR(max), convert(VARBINARY(max), content)), charindex('<EmbeddedImage Name="logo_1">', convert(VARCHAR(max), convert(VARBINARY(max), content)))
--calculate length of new image string
, 15 + charindex('9k=</ImageData>', convert(VARCHAR(max), convert(VARBINARY(max), content))) --endpoint of new image string
- charindex('<EmbeddedImage Name="logo_1">', convert(VARCHAR(max), convert(VARBINARY(max), content))) -- beginning point of new image string
)
FROM reportserver.dbo.CATALOG
WHERE content IS NOT NULL
AND PATH LIKE '%/Live.Reports%'
AND NAME LIKE 'rpt_Triage_new%'
-- Replace <EmbeddedImage> portion of RDL
UPDATE ReportServer.dbo.CATALOG
SET CONTENT = convert(IMAGE, convert(VARBINARY(max), SUBSTRING(convert(VARCHAR(max), convert(VARBINARY(max), content)), 0, charindex('<EmbeddedImage Name="logo">', convert(VARCHAR(max), convert(VARBINARY(max), content)))) + #NewImageString + SUBSTRING(convert(VARCHAR(max), convert(VARBINARY(max), content)), 28 + charindex('53KOZjtohn0ICAA7</ImageData>', convert(VARCHAR(max), convert(VARBINARY(max), content))), 200000)))
WHERE PATH LIKE '%/Test1.Reports%'
AND NAME LIKE 'rptTriage_to_update%'
-- Update <Value> for Image1 reference
UPDATE ReportServer.dbo.CATALOG
SET content = convert(IMAGE, convert(VARBINARY(max), REPLACE(convert(VARCHAR(max), convert(VARBINARY(max), content)), '<Value>logo</Value>', '<Value>logo_1</Value>')))
WHERE content IS NOT NULL
AND PATH LIKE '%/Test1.Reports%'
AND NAME LIKE 'rptTriage_to_update%'
AND convert(VARCHAR(max), convert(VARBINARY(max), content)) LIKE '%<Value>logo</Value>%'
If your reports are not organized in many folders and if you can access your reports as http://server/reports, then delete all existing reports in the folder and upload all the new rdl files. That will make upload rdl files easy.
My final solution involved using the script above to change the report definitions, then use RSScripter to bulk download and upload the reports. I didn't realize it before, but RSScripter automatically generates a command file to re-upload the reports.
I have a financial system where I create pdf forms for tax forms, receipts and etc
I have a printing page where I open the document for the client in an iframe
which suits dynamically the src to the client's pdf -
curUser = usrSrv.getUserFromCookie(cookie);
string formSrc = "UserForms/" + curUser.Id + ".pdf";
ifPdf.Attributes.Add("src", formSrc);
iI my code behind I've inserted the clear cache properties as such:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
but still - in several cases (when the user goes back from the print page for ex') - the pdf file is being saved in the cache and the system is losing its purpose.
I've figured out there might be a way with - server.Mappath() - but when I use it - the location seems fine and the file exists but the browser never finds the actual file or simply don't show it.
If you add a querystring parameter to the end of the frame's URL you will get the result you need, as long as the parameter is generated fresh and unique every time. A common way of doing that is to add something like a timestamp:
url += "?ts=" + DateTime.Ticks;
or:
url += "?ts=" + Date.getTime();
I am developing image extraction application in .net using C# in VS2010.
i have created a path ,where the image will be extracted.But this path is specific to my system.
string image1 = "c:\\Users\\Raghu\\Desktop\\r.bmp";
I want a path which should be general i.e when the project will be deployed ,the output file should be extracted in Target Users desktop.
how create a folder on desktop and and all my extracted files goes in it.
Any ideas! please help me!!
Next code will return path to the desktop of current user:
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
So, in your case it would be
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string image1 = System.IO.Path.Combine(desktop, "r.bmp");
Environment.SpecialFolder (http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx) contains many definitions of system folder paths. Take a look which you need.
You would use the DesktopDirectory for Environment.SpecialFolder. Something like this:
public static string GetDesktopDirectory()
{
return Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
}
Then using the result of that method, you can use Path.Combine to append a file name to it.
var myFilePath = Path.Combine(GetDesktopDirectory(), "r.bmp");
Path.Combine is the general solution for this, as directly concating strings may result in double slashes, etc. This takes care of that for you.
I have an Image object.
I have the Picture type set to linked, so I can change the picture if I want.
I have the Picture property set to the picture name.
I would think that access would use relitive addressing and simple looking in the current directory for the image. But it does not and I get an error telling me it cannot find the picture.
Anyone have a solution? (Other than setting the Picture type to embedded or using the full file address?)
Thanks!
Update:
Tried this:
Private Sub Form_Load()
Dim file As String
file = CurrentDb().Name
file = Replace(file, ".mdb", ".bmp")
Me.Image46.Picture = file
End Sub
It works, except I still get the error message. I click O.K. and it works. Just need the error message to go away.
SOLUTION: Use the above code (or the code posted in the answer below) and then set the 'picture type' to "embedded" and then delete the 'picture' field so that it says "(none)".
Save and run.
It should work.
THANKS!
You could set the property on the forms OnLoad event like this
Me.imgMy_image.picture=getDBPath & “mypicture.bmp”
Here is the getDBPath function
Public Function GetDBPath() As String
Dim strFullPath As String
Dim I As Integer
strFullPath = CurrentDb().Name
For I = Len(strFullPath) To 1 Step -1
If Mid(strFullPath, I, 1) = "\" Then
GetDBPath = Left(strFullPath, I)
Exit For
End If
Next
End Function
Before anyone comments yes I know in access 2000 and above you can use currentproject.path but I’m stuck in the land that time forgot so need that custom function, it still works with later versions of access
Current folder depends of the way you open database in Access. At least, if you open it thru "File-Open", current folder changes to the folder of MDB file. But if you open via double-clicking MDB in explorer, it does not.