/**
* @author Jakey
*
* @brief Download file
*
* @param paramdic additional post parameters
* @param requesturl Request Address
* @param savedpath saved in the disk location
* @param success Download Successful callback
* @param failure Download failed callback
* @param progress Real-time download progress callback
*/
-(void) Downloadfilewithoption: (Nsdictionary *) paramdic
Withinferface: (nsstring*) Requesturl
Savedpath: (nsstring*) Savedpath
Downloadsuccess: (void (^) (afhttprequestoperation *operation, id responseobject)) success
Downloadfailure: (void (^) (afhttprequestoperation *operation, Nserror *error)) failure
Progress: (void (^) (float progress)) Progress
{
Sandbox path//nsstring *savedpath = [Nshomedirectory () stringbyappendingstring:@ "/documents/xxx.zip"];
Afhttprequestserializer *serializer = [Afhttprequestserializer serializer];
Nsmutableurlrequest *request =[serializer requestwithmethod:@ "POST" Urlstring:requesturl parameters:paramDic Error: NIL];
The following is a manual creation of the request method afquerystringfromparameterswithencoding sometimes saved
Nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:requesturl];
Nsmutableurlrequest *request =[[[afhttprequestoperationmanager manager]requestserializer]requestwithmethod:@ " POST "Urlstring:requesturl Parameters:paramaterdic Error:nil];
//
NSString *charset = (__bridge NSString *) Cfstringconvertencodingtoianacharsetname ( Cfstringconvertnsstringencodingtoencoding (nsutf8stringencoding));
//
[Request setvalue:[nsstring stringwithformat:@ "application/x-www-form-urlencoded; charset=%@", CharSet] forhttpheaderfield:@ "Content-type"];
[Request sethttpmethod:@ "POST"];
//
[Request Sethttpbody:[afquerystringfromparameterswithencoding (Paramaterdic, nsasciistringencoding) Datausingencoding:nsutf8stringencoding]];
Afhttprequestoperation *operation = [[Afhttprequestoperation alloc]initwithrequest:request];
[Operation Setoutputstream:[nsoutputstream Outputstreamtofileatpath:savedpath Append:no];
[Operation setdownloadprogressblock:^ (Nsuinteger bytesread, Long long totalbytesread, long long Totalbytesexpectedtoread) {
float p = (float) totalbytesread/totalbytesexpectedtoread;
Progress (P);
NSLog (@ "download:%f", (float) totalbytesread/totalbytesexpectedtoread);
}];
[Operation setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, id responseobject) {
Success (Operation,responseobject);
NSLog (@ "Download succeeded");
} failure:^ (Afhttprequestoperation *operation, Nserror *error) {
Success (OPERATION,ERROR);
NSLog (@ "Download Failed");
}];
[Operation start];
}
Use
-(Ibaction) downloadtouched: (ID) Sender {
NSString *savedpath = [Nshomedirectory () stringbyappendingstring:@ "/documents/qq7.6.exe"];
Nsdictionary *paramaterdic= @{@ "jsonstring": [@{@ "userid": @ "2332"} jsonstring]?:@ ""};
[Self downloadfilewithoption:@{@ "userid": @ "123123"}
withinferface:@ "Http://dldir1.qq.com/qqfile/qq/QQ7.6/15742/QQ7.6.exe"
Savedpath:savedpath
downloadsuccess:^ (afhttprequestoperation *operation, id responseobject) {
} downloadfailure:^ (Afhttprequestoperation *operation, Nserror *error) {
} progress:^ (float progress) {
}];
}
afnetworking block download file Save to sandbox