2015-07-21 87 views
0

我構建了一種將文件中的圖像添加到文件並將其存儲在自定義屬性中的方法。這工作正常。檢查文件是否爲圖像

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 

可悲的是,當我在文件對話框答案添加*我也可以選擇像txt另一種文件類型,然後存儲在自定義屬性,但在圖像不顯示。

如何檢查用戶是否真的選擇了有效的圖像文件?

回答

2

我認爲你可以通過檢查文件擴展名或文件的幻數來驗證。這些信息可以幫助您確保文件是否爲圖像文件。掃描文件內容以驗證它是否爲圖像文件不是一個好主意。

+0

謝謝。我想我可以用 '放入binarydecode(「H8」,tMyImage,tMyImageHex)' ,然後檢查我接受的幾個十六進制值 – Tate83

0

我想通了,我想我會分享整個相關部分:

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