PDA

View Full Version : QFtp:upload file to server



ensky_cy
14th December 2009, 04:00
I am confusing of uploading file to server.
I want know how I specified the path in the server.The file ,that will be uploaded,should be open,and copy to memory,then upload to the file which I created in the server...
May be I don't explain it clear,I just want to konw the process how the file upload to the server.
Thank you!!


int i = 0;
QString filename;
QString files = QFileDialog::getOpenFileName(this, tr("Select File"),
"D://");

if (files.isEmpty())
return;

while(files.indexOf("/", i) != (-1)) //find the file name not include the full url
i = files.indexOf("/",i) + 1;
while(i < files.size())
{
filename.append(files.at(i));
i++;
}

file = new QFile(filename);
Q_ASSERT(file != NULL);

if(!file->open(QIODevice::ReadWrite)) //finished create the file in the debug file
{
qDebug() << "opern err" << file->fileName();
return;
}

ftp->put(file,filename);

After the ftp->put , I find there is no file in the server directory.

ensky_cy
14th December 2009, 05:46
I get the error information:"Uploading file failed:Cannot STOR. No permission.".So I find my ftp server have some problem, I create a anonymous client and have read/write permission, then upload file success.
But the file ,which will upload, should in my debug directory,how I can implement upload the file that in anywhere?The open function neet FILE * fh or int fd,not the file path, like
'D://Qt '.

yogeshgokul
14th December 2009, 06:38
I get the error information:"Uploading file failed:Cannot STOR. No permission.".So I find my ftp server have some problem, I create a anonymous client and have read/write permission, then upload file success.
But the file ,which will upload, should in my debug directory,how I can implement upload the file that in anywhere?The open function neet FILE * fh or int fd,not the file path, like
'D://Qt '.

Then why are you manually removing the full path. I have updated your code. Please try it.Kindly also check what debug messages comes.

int i = 0;
QString filename;
QString files = QFileDialog::getOpenFileName(this, tr("Select File"),
"D://");
if (files.isEmpty())
return;
file = new QFile(filename);
Q_ASSERT(file != NULL);
if(!file->open(QIODevice::ReadWrite))
{
qDebug() << "opern err" << file->fileName();
return;
}
qDebug() << "Upload strat";
ftp->put(file,filename);
qDebug() << "Upload end";

ensky_cy
14th December 2009, 06:53
I have already solved this problem.I use the QDir to change the file directory.

ensky_cy
14th December 2009, 07:01
Then why are you manually removing the full path. I have updated your code. Please try it.Kindly also check what debug messages comes.

int i = 0;
QString filename;
QString files = QFileDialog::getOpenFileName(this, tr("Select File"),
"D://");
if (files.isEmpty())
return;
file = new QFile(filename);
Q_ASSERT(file != NULL);
if(!file->open(QIODevice::ReadWrite))
{
qDebug() << "opern err" << file->fileName();
return;
}
qDebug() << "Upload strat";
ftp->put(file,filename);
qDebug() << "Upload end";


Thank you!
The files is like:"D:/Qt/TCP/xxx.rar",and the filename is "xxx.rar".
We create the file(xxx.rar), not the file(D:/Qt/TCP/xxx.rar).So , I need to pick up the xxx.rar.

yogeshgokul
14th December 2009, 09:05
Thank you!
The files is like:"D:/Qt/TCP/xxx.rar",and the filename is "xxx.rar".
We create the file(xxx.rar), not the file(D:/Qt/TCP/xxx.rar).So , I need to pick up the xxx.rar.

For that you doesn't need to write so much as you have written.
Just 2 lines are enough

QFileInfo fi("/tmp/archive.tar.gz");
QString name = fi.fileName();

ensky_cy
14th December 2009, 10:42
yes ,QFileInfo is useful.