Classic ASP
Classic ASP
Check Whether the Last SFTP Read Failed
See more SFTP Examples
Demonstrates the Chilkat SFtp.LastReadFailed method, which returns whether the most recent read for a handle failed. The only argument is the handle. A normal end-of-file is not a failure.
Background: A chunked read loop needs to tell three outcomes apart: more data, a clean end-of-file, and an actual error such as a dropped connection or a revoked handle.
LastReadFailed isolates the error case — it is false on a normal EOF read (which succeeds with zero bytes) and true for an empty, malformed, or already-closed handle — so the loop can stop cleanly on EOF but bail out with an error message on a genuine failure.Chilkat Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
' Demonstrates the SFtp.LastReadFailed method, which returns whether the most recent read for a
' handle failed. The only argument is the handle. A normal end-of-file is NOT a failure.
set sftp = Server.CreateObject("Chilkat.SFtp")
' Connect, authenticate, and initialize the SFTP subsystem.
port = 22
success = sftp.Connect("sftp.example.com",port)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( sftp.LastErrorText) & "</pre>"
Response.End
End If
' Normally you would not hard-code the password in source. You should instead obtain it
' from an interactive prompt, environment variable, or a secrets vault.
password = "mySshPassword"
success = sftp.AuthenticatePw("mySshLogin",password)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( sftp.LastErrorText) & "</pre>"
Response.End
End If
success = sftp.InitializeSftp()
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( sftp.LastErrorText) & "</pre>"
Response.End
End If
handle = sftp.OpenFile("subdir/data.txt","readOnly","openExisting")
If (sftp.LastMethodSuccess = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( sftp.LastErrorText) & "</pre>"
Response.End
End If
reading = 1
chunkSize = 4096
set bd = Server.CreateObject("Chilkat.BinData")
Do While reading
success = sftp.ReadFileBd(handle,chunkSize,bd)
' Distinguish an actual read failure from a normal EOF. On EOF the read succeeds, returns
' zero bytes, and LastReadFailed is 0.
If (sftp.LastReadFailed(handle)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Read failed: " & sftp.LastErrorText) & "</pre>"
Response.End
End If
reading = Not sftp.Eof(handle)
Loop
success = sftp.CloseHandle(handle)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( sftp.LastErrorText) & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Read " & bd.NumBytes & " bytes with no read failures.") & "</pre>"
sftp.Disconnect
%>
</body>
</html>