Simple JSON HTTP transmission will not say, see a simple demo bar.
The main understanding parameters is the fill parameter, the type is the word typical. I have encapsulated this part of the code so that it can be called multiple times. Maybe write it together more clearly.
#pragmaMark-json mode post submission data-(void) Postjsonwithurl: (NSString *) urlstr parameters: (ID) Parameters Success: (void(^) (IDResponseobject)) Success fail: (void(^) ()) fail{Afhttprequestoperationmanager*manager =[Afhttprequestoperationmanager Manager]; //Set Request FormatManager.requestserializer =[Afjsonrequestserializer Serializer]; //Set return formatManager.responseserializer =[Afhttpresponseserializer Serializer]; [Manager post:urlstr parameters:parameters success:^ (Afhttprequestoperation *operation,IDresponseobject) { //View return Data//nsstring *result = [[NSString alloc] Initwithdata:responseobject encoding:nsutf8stringencoding]; if(Success) {success (responseobject); }} failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@"%@", error); if(fail) {fail (); } }];}
Here is the method of invocation: The returned JSON will have the ID of the variable, the JSONDATAAFN frame automatically converted into a word typical, with the health directly can be taken to the value.
//Jason Parsing, extracting user information from database based on upload ID-(void) jasontest{//URL to accessNSString *url =@"http://localhost:8888/chepaishibie/selectuser.php"; //parameters that need to be passed inNsdictionary *parameter = @{@"DriverID":@"10000"}; //Get or POST request[[Shenafn sharedinstance] Jsondatawithurl:url parameter:parameter success:^ (IDjsondata) { //returns the character, and outputsNSLog (@"success:\n%@", Jsondata); //if it is a different character, you can parse the returned statement with the following statement//nsstring *result = [[NSString alloc] Initwithdata:jsondata encoding:nsutf8stringencoding]; //if it is in JSON format, you can get the variable in this way IDinfo1=jsondata[@"Info1"]; _personname=info1[@"PersonName"]; } Fail:^{NSLog (@"request failed"); }];}
The online implementation of the multi-image upload tutorial is almost no, so here specifically. Easy for others to learn.
First look at the browser-side upload implementation: http://www.w3school.com.cn/php/php_file_upload.asp
In general, the for form is transmitted by default key-value pairs, and transfers are transmitted in binary mode when the form type is set to Enctype= "Multipart/form-data".
PHP background and the same as the general tutorial, I changed a little bit, with foreach can be a dynamic single file upload or multiple file upload.
Note that the upload file is placed in the $_files array, get the way $_files["FILE0"] or $_files["File1", this FILE0 is the name defined in the following iOS project.
PHP background code is as follows:
<?Header("content-type:text/html; Charset=utf-8 "); include(".. /configmysql.php "); mysql_select_db("Chepaidb",$q);//database//post Get key-value pair parameters $TEMPid=$_post[' DriverID ']; Echo"\ndriverid is".$TEMPid." \ n "; foreach($_files as $_eachfile) { //file Processing if($_eachfile["error"] > 0) { Echo"Error:".$_eachfile["Error"]. "\ n"; } Else { Echo"File name:".$_eachfile["Name"]. "\ n"; Echo"Type:".$_eachfile["Type"]. "\ n"; Echo"Size:". ($_eachfile["Size"]/1024). "Kb\n"; } if(file_exists("uploadimage/".$_eachfile["Name"])) { Echo $_eachfile["Name"]. "The file already exists."; } Else { Move_uploaded_file($_eachfile["Tmp_name"], "uploadimage/".$_eachfile["Name"]); Echo"The file has been stored in:". " uploadimage/".$_eachfile["Name"]. " \ n "; } } /*//file file processing if ($_files["file" ["error"] > 0) {echo "Error:". $_files["File" ["Error"]. "\ n"; } else {echo "file name:". $_files["File" ["Name"]. "\ n"; echo "Type:". $_files["File" ["type"]. "\ n"; echo "Size:". ($_files["File" ["Size"]/1024). "Kb\n"; } if (File_exists ("uploadimage/". $_files["File" ["Name"])) {echo $_files["file" ["Name"]. "The file already exists."; } else {move_uploaded_file ($_files["file"] ["Tmp_name"], "uploadimage/". $_files["File" ["Nam E "]); echo "file has been stored in:". " uploadimage/". $_files["File" ["Name"]. " \ n "; } */?>
In the AFN framework, Formdata is the embodiment of [manager post:urlstr Parameters:parameter constructingbodywithblock:^ (id< Afmultipartformdata> formData) {} This function is intended for formData uploads and can carry additional parameters.
Single image upload:
#pragmaMark-Single Image upload-(void) Uploadimgwithurl: (NSString *) URLSTR Image: (UIImage *) image filename: (NSString *) filename success: (void(^) (IDResponseobject)) Success fail: (void(^) ()) fail{//parameters that need to be passed, Jason formatNsdictionary *parameter = @{@"DriverID":@"10000"}; Afhttprequestoperationmanager*manager =[Afhttprequestoperationmanager Manager]; Manager.responseserializer=[Afhttpresponseserializer Serializer]; [Manager post:urlstr Parameters:parameter Constructingbodywithblock:^(ID<AFMultipartFormData>formData) { /*file name reference code//in Web development, when uploading a file, the file is not allowed to be overwritten, the name of the file//To resolve this problem,//can be uploaded using the current system event as the filename Nsdateforma tter *formatter = [[NSDateFormatter alloc] init]; Set the time format Formatter.dateformat = @ "YYYYMMDDHHMMSS"; NSString *str = [formatter stringfromdate:[nsdate date]]; NSString *newfilename = [NSString stringwithformat:@ "%@.png", str]; */[FormData appendpartwithfiledata:uiimagepngrepresentation (image) Name:@"file"Filename:filename MimeType:@"Image/png"]; } Success:^ (Afhttprequestoperation *operation,IDresponseobject) { if(Success) {//NSLog (@ "success\n");success (Responseobject); }} failure:^ (Afhttprequestoperation *operation, Nserror *error) { if(fail) {//NSLog (@ "fail\n");fail (); } }];}
Call:
//Single image upload-(void) uploadimg{NSString*url =@"http://localhost:8888/chepaishibie/uploadimage.php"; UIImage*image=[uiimage imagenamed:@"Swift"]; [[Shenafn sharedinstance] Uploadimgwithurl:url image:image fileName:@"Myswift.png"success:^ (IDjsondata) {NSString*result =[[NSString alloc] Initwithdata:jsondata encoding:nsutf8stringencoding]; NSLog (@"%@", result); } Fail:^{NSLog (@"request failed"); }];}
Multi-image upload: The main use of the Appendpartwithfiledata function, a for loop to add images in the Nsmutablearray.
- (void) Uploadmutableimgwithurl: (NSString *) urlstr Imagearray: (Nsmutablearray *) Imgarray success: (void(^) (IDResponseobject)) Success fail: (void(^) ()) fail{//parameters that need to be passed, Jason formatNsdictionary *parameter = @{@"DriverID":@"10000"}; Afhttprequestoperationmanager*manager =[Afhttprequestoperationmanager Manager]; Manager.responseserializer=[Afhttpresponseserializer Serializer]; [Manager post:urlstr Parameters:parameter Constructingbodywithblock:^(ID<AFMultipartFormData>formData) { /*file name reference code//in Web development, when uploading a file, the file is not allowed to be overwritten, the name of the file//To resolve this problem,//can be uploaded using the current system event as the filename Nsdateforma tter *formatter = [[NSDateFormatter alloc] init]; Set the time format Formatter.dateformat = @ "YYYYMMDDHHMMSS"; NSString *str = [formatter stringfromdate:[nsdate date]]; NSString *newfilename = [NSString stringwithformat:@ "%@.png", str]; */ for(intI=0; I<[imgarray Count]; i++) {[FormData appendpartwithfiledata:uiimagepngrepresentation ([Imgarray objectatindex:i]) name:[nsstring St Ringwithformat:@"file%d", I] filename:[nsstring stringWithFormat:@"Pic%d.png", I] MimeType:@"Image/png"]; }} Success:^ (Afhttprequestoperation *operation,IDresponseobject) { if(Success) {//NSLog (@ "success\n");success (Responseobject); }} failure:^ (Afhttprequestoperation *operation, Nserror *error) { if(fail) {//NSLog (@ "fail\n");fail (); } }];}
Call:
//Multi-image upload-(void) uploadmutableimg{NSString*url =@"http://localhost:8888/chepaishibie/uploadimage.php"; UIImage*image1=[uiimage imagenamed:@"0"]; UIImage*image2=[uiimage imagenamed:@"1"]; Nsmutablearray*imgarray=[[Nsmutablearray alloc]init]; [Imgarray Addobject:image1]; [Imgarray Addobject:image2]; [[Shenafn Sharedinstance] Uploadmutableimgwithurl:url imagearray:imgarray success:^(IDjsondata) {NSString*result =[[NSString alloc] Initwithdata:jsondata encoding:nsutf8stringencoding]; NSLog (@"%@", result); } Fail:^{NSLog (@"request failed"); }];}
In addition the picture compression of JPEG can be used:
UIImageJPEGRepresentation (image,0.5);
Github:https://github.com/rayshen/afndemo
[IOS afnetworking Framework Implementation HTTP request, multi-file image upload DOWNLOAD]