Function: Save data to the specified path according to a URL address, support the continuation of the breakpoint
Parameters: URL--URL address to be accessed
Savepath--The path to be saved
Downedsize already downloaded size
TotalSize Total File size
Return value: Ture--success false--failure
BOOL HttpGet::D ownfile (const qurl &url,const QString &savepath,int downedsize,int totalsize)
{//Create parent folder
QString Curpath=qapplication::applicationdirpath () + "/files";
if (! Qdir (Curpath). Exists ())
{
Qdir Photodir;
Photodir.mkdir (Curpath);
}
Create sub-folders
if (! Qdir (Savepath). Exists ())
{
Qdir Photodir;
Photodir.mkdir (Savepath);
}
Qnetworkrequest Qheader;
Qheader.seturl (URL);
QString range= "bytes" +qstring::number (downedsize) + "-";//Tell the server to start the transfer from Downedsize
Qheader.setrawheader ("Range", Range.toascii ());
Qnetworkaccessmanager Manager;
Reference http://www.qtforum.org/article/31355/qnetworkaccessmanager-using-custom-headers-to-download-a-file.html
Qeventloop Loop;
Qnetworkreply *reply = manager.get (qnetworkrequest (URL));
Qnetworkreply *reply = Manager.get (Qnetworkrequest (Qheader));
Qobject::connect (reply, SIGNAL (finished ()), &loop, SLOT (Quit ()));
Loop.exec ();
Qfileinfo Fileinfo=url.path ();
QFile file (Savepath+fileinfo.filename ());
File.Open (qiodevice::writeonly);
File.write (Reply->readall ());
Delete reply;
return true;
}
Use
Getter. Downfile (Qurl (FILEURL), QString (Currentpath), 0,filesize);
Qobject::connect (&getter, SIGNAL (finished ()), SLOT (Quit ()));
Breakpoint Continuation principle: You need to add the Rang section in the header of the HTTP request to tell the server to start the transfer from that location in the file. The format is bytes start Transfer location
http://blog.csdn.net/henreash/article/details/7267271
QT Breakpoint Continuation (principle: You need to add the Rang section to the header of the HTTP request to tell the server to start the transfer from that location in the file.) Format bytes Start transfer location)