Archived Forum Post

Index of archived forum posts

Question:

FTP uploading from memory instead of local file

Apr 10 '15 at 13:05

I am using the trial dll and trying to upload one image file at a time when the user triggers the code to pass in the filename, hostname,etc. The connection is established, the 'putFileFromBinaryData' test is true, and the file name appears in the correct directory. Unfortunately the uploaded file size is only 34 bytes (or 20 bytes, or 50 bytes) of a much larger jpg file. Even though all 'success' tests are true there is LastErrorText (below) which seems to indicate that it is uploading from memory rather than 'uploadFromLocalFile' which I have seen in other postings. I would appreciate any help to get it to upload the complete local file. The code and the LastErrorText follow. Just as an aside, the complete files upload if I use ftp.PutTree to upload a directory of images, but we want to upload them one at a time on demand. Thank you! Alison

    
    Dim fso, outFile, ftp, success

Set fso = CreateObject("Scripting.FileSystemObject")
    Set outFile = fso.CreateTextFile(localFileName, True)

' This CreateObject statement uses the new single-DLL ActiveX for v9.5.0
    Set ftp = CreateObject("Chilkat_9_5_0.Ftp2")

'  Any string unlocks the component for the 1st 30-days.
    success = ftp.UnlockComponent("Anything for 30-day trial")
    If (success <> 1) Then
        outFile.WriteLine (ftp.LastErrorText)
        WScript.Quit
    End If

ftp.Hostname = host
    ftp.UserName = user
    ftp.password = password

'  Use Passive mode. not
    ftp.Passive = 1

'  Connect and login to the FTP server.
    success = ftp.Connect()
    If (success <> 1) Then
        outFile.WriteLine (ftp.LastErrorText)
        WScript.Quit
    End If

'  Change to the remote directory where the file will be uploaded.
    success = ftp.ChangeRemoteDir("photosNetSpecs")
    If (success <> 1) Then
        outFile.WriteLine (ftp.LastErrorText)
        WScript.Quit
    End If

success = ftp.PutFileFromBinaryData(localFileName, remotefilename)
    If (success <> 1) Then
        outFile.WriteLine (ftp.LastErrorText)
        WScript.Quit
    End If

ftp.Disconnect

MsgBox "File Uploaded!"

outFile.Close
---------------------------------------------------------

This is the LastErrorText:

ChilkatVersion:9.5.0.48
UnlockPrefix: Anything for 30-day trial
Username: ALISON:alison
Architecture: Little Endian; 32-bit
Language: ActiveX
VerboseLogging: 0
ProgressMonitoring:
 enabled: yes
 heartbeatMs: 0
 sendBufferSize: 65536
--ProgressMonitoring
uploadFromMemory:
numBytesToUpload: 34
uploadFromDataSource:
 initialGreeting: 220 Microsoft FTP Service
restartNext: 0
modeZ: 0
binaryMode: 1
setupDataConnection:
 passive transfer mode
 setupPassiveDataSocket:
 sendCommand:
 sendingCommand: PASV

Accepted Answer

You are calling PutFileFromBinaryData which will try to send data from an array in memory, but passing a filename to the content property. If inspected, I suspect your 34 bytes on the server would be the filename that you've passed.

If you want to send a locally stored file by path to the server, then try the PutFile method instead.


Answer

This is a followup to thank you once again for your help, and to let you know that the 'partial upload' issue was directly related to an issue at the office. I found out that my FTP testing happened to coincide with a firewall and security software update that was wreaking havoc with my ftp attempts. Those issues have JUST been resolved this morning, and I am delighted to report that the FTP code is uploading complete files at the office and from home. Your help (with the 'PutFile' instead of 'PutFileFromBinaryData', and the 'outfile' issue) was invaluable, and we owe our current success to you. I will be placing our order at Chilkat soon. Thank you!! Alison :)


Answer

Thank you, jpbro! Using PutFile, the LastErrorText is now displaying upLoadFromLocalFile, but the LocalFileSize is 0 bytes now. I have done lots of testing, and this is what is happening:

I copy a jpg to a directory and check its file size (i.e. 25,000 bytes) I PutFile it to the remote directory. The LastErrorText does say uploadFromLocalFile now, but the LocalFileSize is 0. I check the remote directory and the filename is there, but the bytes are 0. I check the source directory and the filename is there, but the bytes have changed from 25,000 to 0. The source file is being cleared out.

The only thing that changed in the code was PutFileFromBinaryData became PutFile. What could I be doing wrong? Thanks! Alison


Answer

Could the problem be that you are creating your log file using the same path/filename as the file you are trying to upload?

Set outFile = fso.CreateTextFile(localFileName, True)

Answer

Yes! Thank you! I have changed that, and my source files seem to be safe :) Unfortunately I am still not uploading the complete file. This is what is happening now:

I copied a jpg to a directory and checked its file size (109,458 bytes). I PutFile it to the remote directory. The LastErrorText does say uploadFromLocalFile now, and the LocalFileSize is 109,458. I check the remote directory and the filename is there, but the bytes are only 17060. If I view the jpg on the server, I just see the top bit of the image. (The source file still has 109,458 bytes.)

This is extremely interesting, and we are getting closer. I am very grateful for your help. Do you have any more suggestions? :) Ali


Answer

This is the latest code. I have always had to comment out the four Wscript.quit lines to get a clean compile because Access throws a 'variable not defined' error if I leave them in. Could this be the problem? I just thought that they were related to writing the LastErrorText.

Dim fso, outFile, ftp, success

Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile("LogFile", True)

' This CreateObject statement uses the new single-DLL ActiveX for v9.5.0
Set ftp = CreateObject("Chilkat_9_5_0.Ftp2")

'  Any string unlocks the component for the 1st 30-days.
success = ftp.UnlockComponent("Anything for 30-day trial")
If (success <> 1) Then
    outFile.WriteLine (ftp.LastErrorText)
    'WScript.Quit
End If

ftp.Hostname = host
ftp.UserName = user
ftp.password = password

'  Use Passive mode. not
ftp.Passive = 1

'  Connect and login to the FTP server.
success = ftp.Connect()
If (success <> 1) Then
    outFile.WriteLine (ftp.LastErrorText)
    'WScript.Quit
End If

'  Change to the remote directory where the file will be uploaded.
success = ftp.ChangeRemoteDir("photosNetSpecs")
If (success <> 1) Then
    outFile.WriteLine (ftp.LastErrorText)
    'WScript.Quit
End If

success = ftp.PutFile(localFileName, remotefilename)
If (success <> 1) Then
    outFile.WriteLine (ftp.LastErrorText)
    'WScript.Quit
End If

MsgBox ftp.LastErrorText

ftp.Disconnect

MsgBox "File Uploaded!"

outFile.Close

Thank you for any help you can give me. I would love to get this to work. It is so close!! Ali :)


Answer

This is very strange. I just tried the code again from home (I was at the office earlier when only the top strip of the jpgs were uploading), and the complete jpgs are landing in the remote directory! I will try it again in the morning at the office and see if I get whole or partial uploads. I will report again tomorrow. Many thanks for your attention.