Results 1 to 3 of 3

Thread: upload file in c++

  1. #1
    Join Date
    Oct 2015
    Posts
    46
    Thanks
    19
    Platforms
    Windows

    Default upload file in c++

    my source can't upload files
    Qt Code:
    1. #include <QGuiApplication>
    2. #include <QQmlApplicationEngine>
    3.  
    4. #include <QNetworkAccessManager>
    5. #include <QFile>
    6. #include <string>
    7.  
    8. #include <QNetworkReply>
    9. #include <QHttpMultiPart>
    10. #include <QLoggingCategory>
    11. #include <QTextStream>
    12.  
    13. int main(int argc, char *argv[])
    14. {
    15. QGuiApplication app(argc, argv);
    16.  
    17. QHttpMultiPart * data = new QHttpMultiPart();
    18. QHttpPart imagePart;
    19. imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
    20. imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("multipart/form-data; name=\"image\"; filename=\"1.jpg\""));
    21. imagePart.setRawHeader("Content-Transfer-Encoding","binary");
    22. QFile *file = new QFile("c:\\1.jpg");
    23. file->open(QIODevice::ReadOnly);
    24. imagePart.setBodyDevice(file);
    25. data->append(imagePart);
    26. QNetworkAccessManager *manager = new QNetworkAccessManager();
    27. QLoggingCategory::setFilterRules("qt.network.ssl.w arning=false");
    28. manager->post(QNetworkRequest(QUrl("http://localhost/qml/get.php")),data);
    29. return app.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 


    code php:
    Qt Code:
    1. <?php
    2. $target = "upload/" . basename( $_FILES['image']['name']);
    3. if(move_uploaded_file($_FILES['image']['tmp_name'], $target))
    4. {
    5. echo $target.'<br/>'."Uploaded";
    6. } else {
    7. echo "not Uploaded";
    8. }
    9. ?>
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2015
    Posts
    46
    Thanks
    19
    Platforms
    Windows

    Default Re: upload file in c++

    The problem was solved:
    Qt Code:
    1. #include <QGuiApplication>
    2. #include <QQmlApplicationEngine>
    3.  
    4. #include <QNetworkAccessManager>
    5. #include <QFile>
    6. #include <string>
    7.  
    8. #include <QNetworkReply>
    9. #include <QHttpMultiPart>
    10. #include <QLoggingCategory>
    11. #include <QTextStream>
    12.  
    13. int main(int argc, char *argv[])
    14. {
    15. QGuiApplication app(argc, argv);
    16.  
    17. QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    18. // add image
    19. QHttpPart imagePart;
    20. imagePart.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"image\"; filename=\"1.jpg\""));
    21. imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
    22.  
    23. // open file
    24. QFile *file = new QFile("c:\\1.jpg");
    25. if (!file->open(QIODevice::ReadOnly)) {
    26. qDebug() << "# Could not upload file, could not open file";
    27. return false;
    28. }
    29.  
    30. // read file and set data into object
    31. QByteArray fileContent(file->readAll());
    32. imagePart.setBody(fileContent);
    33. multiPart->append(imagePart);
    34.  
    35. // set url
    36. QUrl url("http://localhost/qml/get.php");
    37. QNetworkRequest request(url);
    38.  
    39. QLoggingCategory::setFilterRules("qt.network.ssl.w arning=false");
    40.  
    41. // create network manager
    42. QNetworkAccessManager * manager;
    43. manager = new QNetworkAccessManager();
    44.  
    45. manager->post(request, multiPart);
    46.  
    47. qDebug() << "# Done sending upload request";
    48.  
    49. return app.exec();
    50. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: upload file in c++

    what was the problem ?
    explain it please.

Similar Threads

  1. upload file in qml
    By ravandi in forum Qt Quick
    Replies: 2
    Last Post: 13th June 2018, 14:04
  2. ftp file upload
    By mouni in forum Newbie
    Replies: 11
    Last Post: 8th September 2016, 15:01
  3. Replies: 1
    Last Post: 21st October 2010, 04:59
  4. Qhttp Upload file
    By danny.lesnik in forum Qt Programming
    Replies: 5
    Last Post: 11th December 2009, 09:02
  5. QNetworkRequest file upload -- please help
    By Runtime Technologies in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2009, 15:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.