Problem related to InternetWriteFile


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Problem related to InternetWriteFile

  1. #1
    Join Date
    Sep 2004
    Posts
    14

    Problem related to InternetWriteFile

    Hi,

    I am facing a problem related to InternetWriteFile of WININET API.

    I am in the process of developing a FTP program, which periodically check a local directory and uploads the any few files available in that directory.

    I am using Wininet (internetwritefile) to transfer for the file. I am able to show the progress also. The problem comes only when there is connection error/break while file transfer.

    The execution never stops (probably it doesn't get timed out). How to overcome this problem?
    I want to return an error when the data flow is obstructed for about certain amount of time and restart the entire process after sometime. Currently my application gets hanged.

    Any sort of help is appreciated.

    Here is my code:

    hFile = FtpOpenFile(hConnection, szFileRemote, &H40000000, dwType, 0)
    If hFile = 0 Then
    ErrorOut Err.LastDllError, "FtpOpenFile"
    Txtstr.WriteLine "Unable to open file on remote server"
    GoTo CloseObjects
    End If
    Open CurFile For Binary Access Read As #1
    Size = LOF(1)

    For j = 1 To Size \ 100
    Get #1, , Data
    If (InternetWriteFile(hFile, Data(0), 100, Written) = 0) Then
    Close #1
    ErrorOut Err.LastDllError, "InternetWriteFile"
    GoTo CloseObjects
    End If
    DoEvents
    sum = sum + 100
    Sbar.Panels(1).Text = CStr(Round((sum * 100) / lLocalflsize)) & "%"
    DoEvents
    Pbar1.Value = sum
    Next j
    If Size Mod 100 <> 0 Then
    Get #1, , Data
    If (InternetWriteFile(hFile, Data(0), Size Mod 100, Written) = 0) Then
    Close #1
    ErrorOut Err.LastDllError, "InternetWriteFile"
    GoTo CloseObjects
    End If 'internet file
    End If 'If Size Mod 100 <> 0

    sum = sum + (Size Mod 100)

    Sbar.Panels(1).Text = CStr(Round((sum * 100) / lLocalflsize)) & "%"
    Close #1
    InternetCloseHandle (hFile)


    Thanks
    M.L.Srinivas

  2. #2
    Join Date
    Jun 2005
    Posts
    1
    Please can someone help as to why I can't upload files with InternetWriteFile.

    I can open the connection, authenticate, change to binary and passive mode, traverse to correct directory, open the file on the FTP server, but as soon as I issue the InternetWriteFile command it instantly returns ZERO and I end up with a empty file on the server. Any help is appreciated....here is my code...

    Code:
    Function FTPFile(ByVal HostName As String, _
        ByVal UserName As String, _
        ByVal Password As String, _
        ByVal LocalFileName As String, _
        ByVal RemoteFileName As String, _
        ByVal sDir As String) As Boolean
            
    ' Declare variables
    Dim hConnection, hOpen, hFile  As Long
    Dim iSize As Long
    Dim Retval As Variant
    Dim iWritten As Long
    Dim iLoop As Long
    Dim iFile As Integer
    Dim FileData As String
    
    ' Open Internet Connecion
    hOpen = InternetOpen("HLP", 1, "", vbNullString, 0)
    
    ' Connect to FTP
    hConnection = InternetConnect(hOpen, HostName, INTERNET_DEFAULT_FTP_PORT, UserName, Password, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
    
    ' change directory
    Call FtpSetCurrentDirectory(hConnection, sDir)
    
    ' create file
    hFile = FtpOpenFile(hConnection, RemoteFileName, GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY, 0)
    
    ' check for successful file handle
    If hFile = 0 Then
        FTPFile = False
        GoTo Exit_Function
    End If
    
    'get next file handle number
    iFile = FreeFile
    
    'open local file
    Open LocalFileName For Binary Access Read As iFile
    
    ' set file size
    iSize = LOF(iFile)
          
    'initialise progress meter
    Retval = SysCmd(acSysCmdInitMeter, "Uploading File (" & RemoteFileName & ")", iSize / 1000)
    
    'set string buffer to BUFFER_SIZE and fill with spaces
    FileData = Space(BUFFER_SIZE)
        
    ' Loop file size
    For iLoop = 1 To iSize \ BUFFER_SIZE
            
        'update progress meter
        Retval = SysCmd(acSysCmdUpdateMeter, (BUFFER_SIZE * iLoop) / 1000)
            
        'get file data
        Get iFile, , FileData
          
        ' write chunk to FTP checking for success
        If Not InternetWriteFile(hFile, FileData, BUFFER_SIZE, iWritten) Then
            FTPFile = False
           GoTo Exit_Function
        End If
    
    Next iLoop
    
    ' handle remainder using MOD
    
        'update progress meter
        Retval = SysCmd(acSysCmdUpdateMeter, iSize / 1000)
    
        'set string buffer to REMAINDER and fill with spaces (char 32)
        FileData = Space(iSize Mod BUFFER_SIZE)
    
        'get file data
        Get iFile, , FileData
        
        ' write remainder to FTP checking for success
        If Not InternetWriteFile(hFile, FileData, iSize Mod BUFFER_SIZE, iWritten) Then
            FTPFile = False
            GoTo Exit_Function
        End If
            
    Exit_Function:
    
    ' remove progress meter
    Retval = SysCmd(acSysCmdRemoveMeter)
    
    'close local file
    Close iFile
    
    ' Close Internet Connection
    Call InternetCloseHandle(hFile)
    Call InternetCloseHandle(hOpen)
    Call InternetCloseHandle(hConnection)
    
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center