Unsolved How to upload a file to a server
-
@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; } }
-
@mps298 Are file and uploadManager valid pointers?
Did you try to debug you're code to find out where exactly it crashes? -
@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 ?