Qnetworkaccessmanager Implement FTP upload function, solve the file name is Chinese, Qurl contains Chinese path can not upload the problem

Source: Internet
Author: User
Tags erro

The use of Qnetworkaccessmanager FTP upload function, the Internet has a lot of demo, here to say is, upload the time does not contain Chinese characters in the file name OK all OK. If you upload a file with the Chinese name, there will be a problem.

Check a lot of data, Qurl contains Chinese path solution, online answer surprisingly consistent, tried a lot of not, in fact, the reason is not difficult, is the encoding reason.

Don't say it directly on the code.

void Widget::on_pushbutton_2_clicked ()
{
      Qtextcodec *codec= qtextcodec::codecforname ("GBK"); This must be set  according to the system encoding
    Qstringlist pushlist = Qfiledialog::getopenfilenames (This, tr ("Open File"), "C:\\users\\administrator\\desktop", tr (" Video (*.mp4);; PNG (*png);; JPG (*jpg);; GIF (*gif)));
    if (pushlist.length () = = 0)
    {
        Return
      //Open file selector Select Upload file.
    The Qdebug () << "Filenamepath ..." <<pushlist.at (0);
    QString Filenamepath = pushlist.at (0);
    Qstringlist list = Filenamepath.split ("/");
    QString fileName = list.at (List.length ()-1);
    Qfile txt1 (Filenamepath);
    Txt1.open (qiodevice::readonly);
    Qdebug () << "File Open is" <<txt1.isopen ();
    Qbytearray By_txt=txt1.readall ();
    Txt1.close ();
    Qnetworkaccessmanager *manager = new Qnetworkaccessmanager (this);
    QString str1 = "ftp://192.168.2.19/t/";
    QString name =  qstring::fromlatin1 (Codec->fromunicode (FileName));//These one by one sentences are the key to implementing transcoding, remember, only the filename                                                                       //call for the transcoding.
   QString name = Qstring::fromlatin1 (Filename.tolocal8bit ()); Transfer code
    Qurl URL = Qurl (str1 +name);
    Url.setusername ("MS");
    Url.setpassword ("0p-0p-0p-");
    U.setport (21)
    
    Qnetworkrequest putrequest;
    Putrequest.seturl (URL);
    Qnetworkreply *putreply = Manager->put (Putrequest, by_txt);
    Connect (putreply, SIGNAL (finished ()), this, SLOT (slotfinished ()));
    Connect (putreply, SIGNAL (uploadprogress (Qint64,qint64)), This,slot (Uploadprogress (Qint64,qint64));
    Connect (putreply, SIGNAL (Error (Qnetworkreply::networkerror)), This,slot (Sloterror (Qnetworkreply::networkerror)) ;
}
Update progress bar

void Widget::uploadprogress (Qint64 bytessent, Qint64 bytestotal)
{
    Qdebug () << "Bytestotal >>>> bytessent .... "<<bytesTotal<<bytesSent;
    Ui->progressbar->setmaximum (bytestotal);
    Ui->progressbar->setvalue (bytessent);
}

Error function

void Widget::sloterror (Qnetworkreply::networkerror erro)
{
    Qnetworkreply *reply = (qnetworkreply *) sender ();
    if (reply->error () = = Erro)
    {
        Qdebug () << "Error!!!!!!!!!!!!!!!!!!" <<qstring::fromutf8 (Reply->readall ());
    }
}
Transfer completion function

void Widget::slotfinished ()
{
    Qnetworkreply *reply = (qnetworkreply *) sender ();
    if (reply->error () = = Qnetworkreply::noerror)
    {
        Qdebug () << "succed!";
    }
    Else
    {
        Qdebug () << "Check & Retry!!!!!!!";
    }
}
Select a "MP4" file to the FTP server.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.