How to upload a file to a server
-
Hi everybody,
I'm asking the community for some help.
My task is to upload a file to an http server. I know about QNetworkAccess and put/post methods, but I'm apparently struggling with pointing correct pathes, names, addresses etc and can't request uploading correctly, therefore my app crashes.
We have:- A file named input.txt, its absolute path is D:\files\input.txt (Windows 10 64 bit);
- An http server with address, say123.abc.com.
I need this input.txt file to get uploaded and appear at 123.abc.com/input.txt
If it can be done without stuff like creating QByteArray or something, directly sending the txt file as is, it would be just brilliant.
Please, give me some good example how I should code it. The Internet is only full of download examples, looks as if nobody uploads files)) really looking forward to your help.
Many thanks and all the best!
-
Hi,
- What does that server do ? Is it ftp/sftp/S3 ?
Does it have an end point that supports upload ?
As for the file appearing at that specific address, it's the responsibility of the server to provide it correctly so unless you're using something like cutelyst to implement the server side, it's not a Qt question.
- What does that server do ? Is it ftp/sftp/S3 ?
-
@mps298
do you mean form upload (like it would have been done from a HTML form)?
If so take a look at QHttpMultiPart class -
@SGaist said in How to upload a file to a server:
- What does that server do ? Is it ftp/sftp/S3 ?
Does it have an end point that supports upload ?
It’s Nginx http server. It supports uploading as much as I know
What do you think my upload request should look like? Thank you
- What does that server do ? Is it ftp/sftp/S3 ?
-
@mps298 said in How to upload a file to a server:
What do you think my upload request should look like?
you should tell us!
What do you want to achieve?! What application runs on the server side? ... -
@mps298 said in How to upload a file to a server:
It Nginx http server. It supports uploading as much as I know
That's not enough, you need to know exactly how your server is expecting the upload request. Whether it's the nginx upload module or an application that is served by nginx, what type of request it should be, what's the URL the request should be sent to, etc.
-
@raven-worx said in How to upload a file to a server:
What do you want to achieve?!
As I wrote at the very beginning,
I need this input.txt file to get uploaded and appear at 123.abc.com/input.txt
That’s all I need to do, whether somehow copy the file from my computer to the server or copy a text from my file to an empty input.txt file which exists on the server (I can create it on the server in advance if necessary)
I’m sorry, I don’t know how else I might describe it.What application runs on the server side? ...
Some python script is supposed to use this uploaded input.txt file, I don’t know details, this is not my job.
-
@SGaist said in How to upload a file to a server:
That's not enough, you need to know exactly how your server is expecting the upload request. Whether it's the nginx upload module
Yes, it is, no additional services are used for uploading.
-
@mps298 said in How to upload a file to a server:
Some python script is supposed to use this uploaded input.txt file, I don’t know details, this is not my job.
then you need to clarify what that python script expects exactly.
-
@raven-worx, this script needs input.txt file to exist, it uses text from the file for some purpose.
I get a similar txt file from that server easily, just like that:
void MainWindow::startDownloadSlot() { downloadManager->get(QNetworkRequest(QUrl(serverAddress+downloadFileName))); } void MainWindow::downloadFinishedSlot(QNetworkReply *reply) { if (reply->error()) { const QString error = reply->errorString(); qDebug()<<error; return; } const QByteArray content = reply->readAll(); ui->outputTextEdit->setPlainText(content); reply->deleteLater(); }
Why can't I upload a file back in a similar way?
-
As @raven-worx already suggested, use QHttpMultiPart to build your upload query and post it to your server upload path.
-
I tried doing as below, but it successfully crashes the app every time, sending even no error reply...
void MainWindow::startUploadSlot(QFile *file) { if (file->open(QIODevice::ReadOnly)) { QHttpMultiPart multiPart(QHttpMultiPart::FormDataType); QByteArray textBody; while (!file->atEnd()) { textBody.append(file->readLine()); } QHttpPart textPart; textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\"")); textPart.setBody(textBody); multiPart.append(textPart); uploadManager->put(QNetworkRequest(QUrl(serverAddress)), &multiPart); file->close(); return; } }
-
@jsulm, yes, both pointers are valid.
If I transfer a default QUrl() to the post method, it works, no crashes. By "works" I mean it sends me at least an error reply "Protocol is unknown". But when I use the real QUrl, it crashes, but later, not when the post method is working, and I can step over it when debugging, too, but in 7-10 seconds my app crashes
-
What is that URL ?
What does your server log says ?
How is it configured ?