Thread: InternetWriteFile

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    13

    Angry InternetWriteFile

    How do I upload a file with InternetWriteFile over FTP. I know I have to use FTPOPENFILE than use internetwritefile, but it is not working. I need a working example....

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If using WinINet, you would call FtpPutFile(). See the example code here: FTP Sessions (Windows)

    gg

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    13
    The reason I wanted to use INTERNETWRITEFILE was to use a progress bar when I upload a file from my computer so any help with that would be cool

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    13
    here is some code I found, but I also need it to download
    Code:
    #include <winInet.h>
    #include <string>
    #include <windows.h>
    //#include <sstream>
     
    std::string getURL(const char *URL)
    {
        //Constant to hold the read size(4k).
        const int downloadBufferSize = 4096;
     
        //Constant to hold an empty string.
        const std::string errorString = "ERROR";
        //
        //Attempt to open a internet connection. Check for success.
        //
        HINTERNET hInternet = InternetOpen("GINA: Version 0.1", INTERNET_OPEN_TYPE_DIRECT, NULL, 0,0);
        if(hInternet == NULL){
            return errorString;
        }//if
     
        //
        //Attempt to open the url. Check for success
        //
        HINTERNET hFile = InternetOpenUrl(hInternet, URL, NULL, 0, 0, 0);
        if(hFile == NULL){
            return errorString;
        }//if
     
        /*
        //
        //These will be used to hold the content size(Potentially)
        //
        DWORD sizeBuffer;
        DWORD length = sizeof(sizeBuffer);  
     
        //Attempt to grab the content length of the request.
        bool succeeds = HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &sizeBuffer, &length, NULL) == TRUE;
     
        //Special Case: If the query fails handle failure case, Else output the content length.
        if (!succeeds){
            //Grab the last error that occured.
            DWORD lastError = GetLastError();
     
            //Special Case: Header not found is expected. Not all pages output their content length.
            if(lastError != ERROR_HTTP_HEADER_NOT_FOUND){
                std::stringstream outputStream;
                outputStream<<"Error: "<<lastError;
     
                MessageBox(NULL, outputStream.str().c_str(), "Query Info Error", MB_OK);                        
            }//if
        }//if
        */
         
        //Will be used to store all of the downloaded data.
        std::string downloadedContents = "";    
     
        //This will be used to hold the buffer to download.
        char *downloadBuffer = new char[downloadBufferSize];
        DWORD availableSize = 0;
        //Will be used to hold the number of bytes read.
        DWORD bytesRead = 0;    
        do{
              //query how much data is available
              InternetQueryDataAvailable(hFile,&availableSize,0,0);
              //read only as much as is available or downloadBufferSize
              if (availableSize > downloadBufferSize)
                 availableSize = downloadBufferSize;
              //Read the current bytes from the internet file.
              InternetReadFile(hFile, downloadBuffer, availableSize, &bytesRead);
               
              //Append the buffer to the download contents
              downloadedContents.append(downloadBuffer, availableSize);
        }while(bytesRead != 0);
         
        //
        //Close the handles.
        //
        InternetCloseHandle(hFile);
        InternetCloseHandle(hInternet);
        //Return the downloaded contents to the caller.
        return downloadedContents;
    }

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> ... so any help with that would be cool
    Quote Originally Posted by the link I provided
    To upload or place files on an FTP server, the application can use either FtpPutFile or FtpOpenFile (along with InternetWriteFile). FtpPutFile can be used if the file already exists locally, while FtpOpenFile and InternetWriteFile can be used if data needs to be written to a file on the FTP server.
    I would read the the stuff in that link.

    gg

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    13
    My question is...can this be fixed to work right??????????????????Iread the stuff in the link but nothing about progress bars but im think im on the edge of getting this to work right
    Code:
    int index;
             CString strText;
             index = m_dir.GetCurSel();
    
             m_dir.GetText(index,strText);
            
    
    	
    		 HINTERNET handle1 = FtpOpenFile(hIConnect,strText,GENERIC_READ,FTP_TRANSFER_TYPE_BINARY,0);
    		DWORD filesized =  FtpGetFileSize(handle1,NULL);
    		CString csNumber;
    csNumber.Format("%lu", filesized);
    
    MessageBox(csNumber,csNumber,MB_OK);
    HANDLE hFile    = CreateFile(strText, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    char Buffer[4024];
      DWORD dwRead =0;
     
      
    
    
    m_yay.SetRange(0,filesized);
      while(InternetReadFile(handle1, Buffer, sizeof(Buffer), &dwRead) == TRUE)
      {
        if ( dwRead == 0) 
          break;
        DWORD dwWrite = 0;
    	progress2 += dwRead;
    
    	m_yay.SetPos(progress2);
        WriteFile(hFile, Buffer, dwRead, &dwWrite, NULL);
      }
    that code above is for a FTP CLIENT using WININET. the code is getting a file from the server and downloading...it works, but the progress bar is not working right....like it fills to fast...sometimes it doesn't even move...so can you help me with this problem

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    13
    anyways.....

    I added this instead of setpos
    Code:
     m_yay.StepIt();
    the progress bar goes crazy..it does not start from the beginngin..it fills all the waylike 1 million times....but it stops when the download is done...I need to to start from the beginning and increment real slow to the end.
    Last edited by terryeverlast; 07-03-2013 at 11:46 PM.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Settings for the Progress Control
    Based on that, the logic in post #6 looks correct - assuming progress2 was initialized to 0.

    Your problem may be due to using the progress control in a non-UI worker thread. Typically you would send a custom message to the UI thread so it can set the progress control.

    Or perhaps you have no extra threads, which would mean that messages aren't getting pumped during that while loop.

    gg

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Cross-poster
    ftp file size

    Answer: the max range for SetRange() is 0-65535.

    gg

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    13
    sorry for cross posting..i didn't know it was illegal until now....probally wont do it again

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If you've solved your issue, post a resolution in the other thread for future readers/searchers.

    gg

Popular pages Recent additions subscribe to a feed