cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
SamPo
Impactful Individual
Impactful Individual

Detect file content type - if image or not

Hi,

 

I have an entity with attachments stored in CDS (in notes). Some of these files are images and others are not.

I'm using a gallery and an image control to display these attachments in an app. For the files that are images this works well but for those that are not images, the control just shows blank.

 

Does anyone know if there's a way to detect if the file is an image or not? (then I can say 'if not image, use SampleImage')

I know I can write a formula and match it to the file extension (e.g. if extension <> ".jpg" then...) but I'd rather not have to keep a list of every valid image type and want to know if there's a way to avoid this. 

 

Thanks

Sam

1 ACCEPTED SOLUTION

Accepted Solutions
v-xida-msft
Community Support
Community Support

Hi @SamPo ,

Do you want to detect if the file is a image type within your canvas app?

 

I assume that you only upload single one file for each item in your CDS Entity, each of these uploaded files is valid. Based on the needs that you mentioned, I agree with your thought almost, you could consider check the file extension for each uploaded file.

 

Within your Gallery, please set the Image property of the Image control to following:

If(
   Last(Split(First(ThisItem.Attachments).DisplayName, ".")).Result = "jpg" || Last(Split(First(ThisItem.Attachments).DisplayName, ".")).Result = "png" || Last(Split(First(ThisItem.Attachments).DisplayName, ".")).Result = "jpeg",
   First(ThisItem.Attachments).Value,
   SampleImage
)

 

Please consider take a try with above solution, then check if the issue is solved.

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
eka24
Super User
Super User

Watch https://youtu.be/Lg1igV8an2o?t=4 if it can help.

------------

If you like this post, give a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users find it.

v-xida-msft
Community Support
Community Support

Hi @SamPo ,

Do you want to detect if the file is a image type within your canvas app?

 

I assume that you only upload single one file for each item in your CDS Entity, each of these uploaded files is valid. Based on the needs that you mentioned, I agree with your thought almost, you could consider check the file extension for each uploaded file.

 

Within your Gallery, please set the Image property of the Image control to following:

If(
   Last(Split(First(ThisItem.Attachments).DisplayName, ".")).Result = "jpg" || Last(Split(First(ThisItem.Attachments).DisplayName, ".")).Result = "png" || Last(Split(First(ThisItem.Attachments).DisplayName, ".")).Result = "jpeg",
   First(ThisItem.Attachments).Value,
   SampleImage
)

 

Please consider take a try with above solution, then check if the issue is solved.

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

SamPo
Impactful Individual
Impactful Individual

@v-xida-msft Thank you.

Yes this is the solution I was thinking of using. I will store all valid file types in an expression then check if the current file is in the list.

I was just wondering if there was a 'cleaner' way to do this but will go with your solution for now.

 

Thanks

If(
   Last(Split(First(ThisItem.Attachments).DisplayName, ".")).Result in ["jpg","png","jpeg"],
   First(ThisItem.Attachments).Value,
   SampleImage
)

 

Probably this should work also if I'm right. This would be easier to read and extend if needed.

 

Paul

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (1,727)