<%
'---------------------------------------------
'Function name: ChkImg
'Function: Check whether the image file is legal
'Parameters: img, image path, absolute path relative to the website root directory
'Return value: Boolean type, if the picture is legal, return True, otherwise return False
'Condition: The server must support the AspJpeg component,
'If it is not supported, in order to avoid that all pictures cannot be uploaded, this function will directly return True
'http://www.downcodes.com/asp.asp
'---------------------------------------------
Function ChkImg(img)
On Error Resume Next 'In order to capture error information, the code needs to continue executing when an error occurs
Dim RetunValue, ChkJpeg
RetunValue = True
'If the path is empty, the image is considered illegal
If isnull(img) Then ChkImg = False:Exit Function
Set ChkJpeg = Server.CreateObject("Persits.Jpeg")
If -2147221005 <> Err Then 'If the component is supported, use the component to check the legality of the image
ChkJpeg.Open Server.mappath(img)
If Err Then
RetunValue = False
End If
Else 'If the component is not supported, skip and return True directly
RetunValue = True
End If
'Necessary aftermath work
If Err.number <> 0 Then Err.clear
Set ChkJpeg = Nothing
ChkImg = RetunValue
End Function
%>
Note: Because AspJpeg can only process files that are already on the server, we need to do this during actual application: first upload the image to a temporary folder, and then check the legality of the image. If it is legal, copy the image to the image save directory, delete the temporary file and return the upload success message. If it is illegal, delete the temporary file directly and return an error warning.
Because the image is only opened with AspJpeg and no image processing is performed, the execution efficiency of this function is still very high, and there is basically no need to worry about efficiency issues during use.