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.