Este codigo consigue subir el archivo a un ftp que monte en mi pc, pero al probarlo con
Hostinger no hace nada...
Alguna explicacion logica?
#include "string.h"
#include <iostream>
#include <windows.h>
#include <wininet.h>
using namespace std;
#pragma comment(lib,"Wininet.lib")
#include <windows.h>
#include <iostream>
#include <fstream>
#define ID_MYAPP 1
using namespace std;
long lFileSize = 0;
long nSumBytes = 0;
void GetFileName (char* szSource)
{
char* ptr;
for (ptr = &szSource [strlen (szSource)]; *ptr != '\\'; ptr--);
ptr++;
int i;
for (i = 0; *ptr != '\0';ptr++,i++)
szSource[i] = *ptr;
szSource [i] = '\0';
}
int main (int argc, char* argv [])
{
cout << "File Uploading Utility :- " << endl;
HINTERNET hInternet = NULL, hConnection = NULL;
hInternet = InternetOpen(L"Title", 0, NULL, NULL, 0);
if (hInternet == NULL)
{
cout << "Error in opening connection" ;
return 0;
}
hConnection = InternetConnect (hInternet, L"31.170.165.195", 21, L"usuario", L"password", INTERNET_SERVICE_FTP, INTERNET_FLAG_ASYNC, ID_MYAPP);
if (hConnection != NULL)
{
cout << "Connection Established\n";
}
else
{
InternetCloseHandle(hInternet);
}
HINTERNET hFile = NULL;
hFile = FtpOpenFile(hConnection, L"\\archivo remoto", GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY, 0);
HANDLE hLocalFile = NULL;
hLocalFile = CreateFile (L"archivo local", GENERIC_READ , 0, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hLocalFile == INVALID_HANDLE_VALUE) {
cout << "Please check the file name and path" << endl;
return 0;
}
lFileSize = GetFileSize (hLocalFile, NULL) ;
char *inBuffer = new char [lFileSize];
BOOL bResult;
DWORD nBytesRead;
DWORD nBytesToReadWrite = 512; //lFileSize;
DWORD dwError;
DWORD nBytesWritten = 0;
DWORD nBytesLeft = lFileSize;
do
{
if (ReadFile(hLocalFile, inBuffer, nBytesToReadWrite, &nBytesRead, NULL))
{
nSumBytes = nSumBytes + nBytesRead;
bResult = InternetWriteFile (hFile, inBuffer, (nBytesToReadWrite >= nBytesLeft ? nBytesLeft : nBytesToReadWrite), &nBytesWritten);
nBytesLeft = lFileSize - nSumBytes;
cout << "Uploading : " << (nSumBytes * 100) / (lFileSize) << "% Completed" << "\r";
}
else
{
dwError = GetLastError ();
}
} while (nSumBytes != lFileSize);
if (nSumBytes == lFileSize)
cout << "File uploaded successfully" << endl;
CloseHandle(hLocalFile);
InternetCloseHandle (hFile);
InternetCloseHandle (hConnection);
InternetCloseHandle (hInternet);
system("pause");
return 0;
}
Depuralo y fijate que devuelve cada funcion (y el GetLastError cuando corresponda).
PD. No abras mas hilos, todos son sobre lo mismo y el proximo lo borro.
De aki y de alla...
Este codigo sube a la perfeccion archivos binarios a un ftp.
#include <windows.h>
#include <stdio.h>
#include <wininet.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"Wininet.lib")
/*
InternetOpen - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/internetopen.asp
InternetConnect - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/internetwritefile.asp
FtpOpenFile - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/ftpopenfile.asp
InternetWriteFile - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/internetwritefile.asp
InternetCloseHandle - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/internetclosehandle.asp
*/
int main(int argc, char *argv[])
{
HINTERNET Open=InternetOpen(L"Explorador"/*user agent*/,
0/*tipo de conexion*/,
NULL/*nombre del proxy*/,
NULL/*lista opcional de ip y hosts*/,
0/*flag*/
);
if(Open==NULL){printf("\n[*] Error al abrir la conexion\n");return -1;}
printf("Abriendo conexion..\n");
HINTERNET Connect=InternetConnect(Open/*el handle a la sesion*/,
L"localhost"/*la ruta*/,
21,//INTERNET_DEFAULT_FTP_PORT/*el puerto*/,
L"toni"/*el nombre de usuario*/,
L"toni"/*la contraseña*/,
INTERNET_SERVICE_FTP/*tipo de servicio*/,
0/*flag*/,
0);
if(Connect==NULL){InternetCloseHandle(Open);printf("\n[*] Error al conectar con el servidor\n");getchar();return -1;}
printf("Conexion establecida...\n");
printf("Creando archivo...\n");
HINTERNET OpenFile=FtpOpenFile(Connect,/*la sesion ftp*/
L"PORFIN.EXE",/*el nombre del fichero en el servidor*/
GENERIC_WRITE,/**tipo de acceso al archivo*/
FTP_TRANSFER_TYPE_ASCII,/*tipo de transferencia de datos*/
0);
if(OpenFile==NULL){InternetCloseHandle(Open);InternetCloseHandle(Connect);printf("\n[*] Error al crear el fichero en el servidor\n");getchar();return -1;}
printf("Subiendo archivo...\n");
//escribimos el archivo
HANDLE Archivo = CreateFile (L"c:\\texto.txt", GENERIC_READ , 0, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (Archivo == INVALID_HANDLE_VALUE) {
cout << "Please check the file name and path" << endl;
return 0;
}
int lFileSize;
lFileSize = GetFileSize (Archivo, NULL) ;
char *inBuffer = new char [lFileSize];
BOOL bResult;
DWORD nBytesRead;
DWORD nBytesToReadWrite = 512; //lFileSize;
DWORD dwError;
DWORD nBytesWritten = 0;
DWORD nBytesLeft = lFileSize;
bool nSumBytes=0;
do
{
if (ReadFile(Archivo, inBuffer, nBytesToReadWrite, &nBytesRead, NULL))
{
nSumBytes = nSumBytes + nBytesRead;
bResult = InternetWriteFile (OpenFile, inBuffer, (nBytesToReadWrite >= nBytesLeft ? nBytesLeft : nBytesToReadWrite), &nBytesWritten);
nBytesLeft = lFileSize - nSumBytes;
cout << "Uploading : " << (nSumBytes * 100) / (lFileSize) << "% Completed" << "\r";
}
else
{
//dwError = GetLastError ();
}
} while (nSumBytes != lFileSize);
if (nSumBytes == lFileSize)
cout << "File uploaded successfully" << endl;
//salimos
InternetCloseHandle(Open);
InternetCloseHandle(Connect);
InternetCloseHandle(OpenFile);
return 0;
}
Porsierto.... disculpa pero los post son formas diferentes de recoger datos como el ftpgetfile y ftpputfile. este codigo da un porcentaje.