Results 1 to 7 of 7

Thread: upload an executable file using QNetworkAccessManager

  1. #1
    Join Date
    Mar 2011
    Location
    Greece
    Posts
    23
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default upload an executable file using QNetworkAccessManager

    I want to upload an executable file to the server.My code is this:
    Qt Code:
    1. QString data;
    2. QByteArray dataToSend; // byte array to be sent in POST
    3. QFile *inputFile=new QFile("putty.exe");
    4.  
    5. if(!inputFile->open(QIODevice::ReadOnly))
    6. return;
    7.  
    8. data+="\nContent-Disposition: form-data; ";
    9.  
    10. data+=QString("name=\"%1\"; ").arg(name);//putty
    11. data+=QString("filename=\"%1\";").arg(filepath);//http://localhost/uploadFiles/sdda/putty.exe
    12. data+="Content-Type: application/octet-stream" +inputFile->readAll();
    13.  
    14. dataToSend=data.toUtf8(); // convert data string to byte array for request
    15.  
    16. // request init
    17. QNetworkRequest request(QUrl("http://localhost/uploadFile.php"));
    18. request.setRawHeader("Content-Type"," multipart/form-data; boundary=\"-----------------------------7d935033608e2\"");
    19. request.setHeader(QNetworkRequest::ContentLengthHeader,dataToSend.size());
    To copy to clipboard, switch view to plain text mode 

    Running this code the result is:a warning that informs me that a folder in which i want to upload the file already exists.Is the above approach correct?

    //php script

    Qt Code:
    1. <
    2. ?php
    3.  
    4.  
    5. $packageName = trim($_POST['packageName']);
    6. mkdir("uploadFiles/$packageName");
    7. // Upload file
    8. move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'], "uploads/$packageName/{$_FILES['uploadFile'] ['name']}")
    9.  
    10. ?>
    To copy to clipboard, switch view to plain text mode 

    Thanks!
    Last edited by milli; 31st May 2011 at 01:36.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: upload an executable file using QNetworkAccessManager

    Where does this warning comes from ? Server or QNAM ?
    If it's from server, try to debug on server side (I don't know how, because I don't know anything about php)
    I don't really know how to help you with your main issue, but you have possibility of another one - a memory leak here:
    Qt Code:
    1. if(!inputFile->open(QIODevice::ReadOnly))
    2. return;
    To copy to clipboard, switch view to plain text mode 
    You do not release the inputFile before exiting the method, should be more like:
    Qt Code:
    1. if(!inputFile->open(QIODevice::ReadOnly)){
    2. delete inputFile;
    3. return;
    4. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: upload an executable file using QNetworkAccessManager

    Your uploading code is surely incorrect. Converting a binary blob to a utf-8 encoded string (taking Unicode as the source encoding) is definitely an incorrect approach.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Mar 2011
    Location
    Greece
    Posts
    23
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: upload an executable file using QNetworkAccessManager

    Problem solved!!For someone else who has similar problem.

    This code works..
    Qt Code:
    1. QString bound;
    2. QString crlf;
    3. QString data;
    4. QByteArray postData;
    5.  
    6. crlf = 0x0d;
    7. crlf.append(0x0a);
    8. data = "--" + bound + crlf + "Content-Disposition: form-data; name=\"uploadedfile\"; ";
    9. data.append(QString("filename=\"%1\";").arg(filePath));
    10. data.append(crlf + "Content-Type: application/octet-stream" + crlf + crlf);
    11.  
    12. postData.insert(0,data);
    13. postData.append(file.readAll());
    14. postData.append(crlf + "--" + bound + "--" + crlf);
    15.  
    16. QUrl url("http://localhost/uploadedFile.php");
    17. QNetworkRequest req(url);
    18. req.setHeader(QNetworkRequest::ContentTypeHeader, tr("multipart/form-data; boundary="));
    19. file.close();
    20. manager->post(req,postData);
    21. connect(manager,SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    To copy to clipboard, switch view to plain text mode 

    And if wou want to delete file from the server then the code is more than a simple:
    Qt Code:
    1. data.append(QString("packageName=%1").arg(ui->softwareNameEdit->text()));
    2. postData.append(data);
    3. QUrl url("http://localhost/deleteUploadFile.php/");
    4. QNetworkRequest req(url);
    5. manager->post(req,postData);
    To copy to clipboard, switch view to plain text mode 

    However,thank's for your answers!!!

  5. #5
    Join Date
    Mar 2011
    Location
    Greece
    Posts
    23
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: upload an executable file using QNetworkAccessManager

    As I mentioned,the process of uploading a file has done using the above code.I want also to make a folder in which i store this file.
    For example,if the name of the file
    putty.exe
    then the name of the file would be
    applications
    .
    I can make the folder using php scripting.My question is how i can give the name of the folder using qt code?
    I can upload the file into the server or give the name of the folder (and the result is the creation of the folder) using post method , but not both of them.
    Any help would be appreciated..!

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: upload an executable file using QNetworkAccessManager

    I suggest you learn how HTTP works with regard to passing data between the client and the server. I'm sure you have seen webpages with forms where more than one piece of data was posted from the form to the server. You're even looking at one of those pages right now.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    milli (3rd June 2011)

  8. #7
    Join Date
    Mar 2011
    Location
    Greece
    Posts
    23
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: upload an executable file using QNetworkAccessManager

    I have made what i wanted...!!!!!!Thanks wysota!!!!!!!!!!!!!!

Similar Threads

  1. FTP Upload using QNetworkAccessManager
    By replax in forum Newbie
    Replies: 2
    Last Post: 30th October 2014, 13:32
  2. Replies: 1
    Last Post: 21st October 2010, 05:59
  3. Replies: 5
    Last Post: 15th June 2010, 08:42
  4. Replies: 5
    Last Post: 20th January 2009, 15:11
  5. upload files using QNetworkAccessManager
    By Raajesh in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2008, 20:43

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.