Reputation: 274
I build a way to add an image to card from a file and store it in a custom property. This works fine.
answer file tImageDialogTranslations with type "Images|jpg|jpeg|png"
if it is not empty then
put it into tMyImagePath
put url("binfile:" & tMyImagePath) into tMyImage
put base64encode( tMyImage) into tBase64ImgData
# store image in cust prop
set the cImageBlob of img "img_collection_picture" to tBase64ImgData
#show image
put base64decode(tBase64ImgData) into tShowImage
set the text of image "img_collection_picture" to tShowImage
end if
Sadly, when I add an *
in the file answer dialog I can also choose another file type like txt
which then is stored in the custom property but not displayed in the image.
How can I check if the user really chose a valid image file?
Upvotes: 0
Views: 261
Reputation: 274
I figured it out and thought I'd share the whole relevant part:
answer file tImageDialogTranslations with type "Images|jpg,jpeg,png,gif"
if it is not empty then
put it into tMyImagePath
put url("binfile:" & tMyImagePath) into tMyImage
put binarydecode("H8",tMyImage,tMyImageHex) -- to check if it is actually an image
put base64encode( tMyImage) into tBase64ImgData
set the itemdel to comma
if tMyImageHex is among the items of "ffd8ffe0,47494638,89504e47" then
### store image in custom property
set the cImageBlob of img "img_collection_picture" to tBase64ImgData
### now display image in field
put base64decode(tBase64ImgData) into tShowImage
set the text of image "img_collection_picture" to tShowImage
else
# do nothing / error 100499
answer "Error 100499: Please choose an image file. "
end if
else
# do nothing since no file was chosen
end if
Upvotes: 0
Reputation: 844
I think you can validate by checking the file extension or file's magic number. Those info can assist you to ensure that the file is an image file or not. Scanning the file content to validate if it is an image file is not a good idea.
Upvotes: 3