afnetworking block download file Save to sandbox

Source: Internet
Author: User

/**
* @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

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.