-1

I've got a problem with programming an easy FTP-Upload in QT. I am currently using QT 5.5 and the QFtp Class is no longer available. So I used the code that is working in my Visual C++ project.

  1. Image: Code from Visual C++ Code from Visual C++

  2. Image: Code in QT Image: Code in QT

  3. Image: Build Error in QT Image: Build Error in QT

Is there a way I can fix this problem? Is it possible to get QFtp working in QT 5.5?

(string fileToUpload is the file path)

(string fileName is the name of the file on the remote machine when it is uploaded)

Thanks in advance!

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
MasterGrey
  • 11
  • 4
  • 5
    Please do not post code as images. _Edit_ your question so it includes the code using the correct markdown. Otherwise it can not be searched and also not copy and pasted for others to try and debug the issue. – Bowdzone Sep 21 '15 at 12:24
  • I think this should help you http://stackoverflow.com/questions/2573834/c-convert-string-or-char-to-wstring-or-wchar-t – SingerOfTheFall Sep 21 '15 at 12:25
  • I tried to paste the code into the text field but it didn'T work properly. – MasterGrey Sep 21 '15 at 12:27
  • I've embedded it for you, but you could have typed out (or copy and pasted) the code in this case! – TheDarkKnight Sep 21 '15 at 12:29
  • You're welcome. Also note that if you want to refer to someone by name, put an '@' before their name and they'll be notified when you mention them. – TheDarkKnight Sep 21 '15 at 12:32

2 Answers2

2

As stated in the Qt 5.4 documentation, there are add-ons that provide FTP and HTTP streams:

https://forum.qt.io/topic/23904/qtftp-and-qthttp-compatibility-add-ons-for-qhttp-and-qftp-classes-in-qt-5

However, I suggest you start using QNetworkManager to do your FTP & HTTP access. Its interface is more complex (essentially all requests are handled asynchronuously) but when you get the hang of it, it's not so difficult.

JvO
  • 3,036
  • 2
  • 17
  • 32
0

Your compilation error is related to conversion of const char* to const wchar_t*. If you are satisfied with FtpPutFileW you can just convert file name to wchar_t*. That can be done using Qt QString: QString::toWCharArray(wchar_t * array) to convert the string to wchar_t array (this function does not append null character) or using direct cast:

reinterpret_cast<const wchar_t *>(qstring.utf16())

Also it possible to use various approaches to conversion of std::string to wchar_t*.

If you need just FTP put operation the QNetworkManager has such function. However, QNetworkManager has limited support for low level FTP: https://stackoverflow.com/a/32568786/4023446

So, for other low level FTP commands it is possible to install manually Qt FTP.

Community
  • 1
  • 1
Orest Hera
  • 6,706
  • 2
  • 21
  • 35
  • Thanks for your answer. I tried it out with QNetworkManager but I get the following error: "variable 'QNetworkRequest uploadFile' has initializer but incomplete type QNetworkRequest uploadFile(uploadurl); ^" – MasterGrey Sep 21 '15 at 13:59
  • @MasterGrey you need `#include ` and also the qt network module should be added to the project (.pro file `QT += network`). – Orest Hera Sep 21 '15 at 14:05
  • Thank you very much, that fixed the error. But what is the second parameter for XXX->put(QNetworkRequest, ???) – MasterGrey Sep 21 '15 at 14:11
  • @MasterGrey For simplicity you can try the [put version with `QByteArray`](http://doc.qt.io/qt-5/qnetworkaccessmanager.html#put-3) that contains the content to send. – Orest Hera Sep 21 '15 at 14:14
  • The program is uploading a file now to the server. But actually the has a size of 0KB. How can I fix this? And how can I specify that the user can choose the file to upload and the name of the file on the remote machine? Thanks in advance! I've createt a QString that contains the text from the TextField. – MasterGrey Sep 21 '15 at 14:26
  • @MasterGrey There are too many answers to those questions to fit a comment length. At first you should be confident that you are using `QNetworkAccessManager` properly. The entire content of the `QByteArray` buffer should be uploaded. If you see zero file size it is possible that the upload is not finished. When you call `->put()` the request is only initiated. The `finished()` signal is triggered on finish. Look for some examples. So, to upload a file you can put its content to such buffer. It is also possible to use `put` version with `QIODevice*`. – Orest Hera Sep 21 '15 at 14:36
  • Is this correct? QString filePath = ui->boxFilePath->text(); QByteArray file = filePath.toUtf8(); – MasterGrey Sep 21 '15 at 15:13
  • @MasterGrey No, it is not if you want copy file content to `QByteArray`. It is only file name. You need to open file and read its data. For example, [`QFile f(filePath); f.open(QIODevice::ReadOnly); QByteArray data = f.readAll();`](http://doc.qt.io/qt-5/qfile.html) – Orest Hera Sep 21 '15 at 15:27
  • Ahhh, now it makes sense to me. Thanks very much. I'll try it out. – MasterGrey Sep 21 '15 at 15:30
  • Ok, this worked fine for me. Thank you very much, Orest. I think I can make the rest on my own. :) – MasterGrey Sep 21 '15 at 15:39