2017-12-24 7 views
0
NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy"; 

// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ 
NSString* FileParamConstant = @"picture"; 

// the server url to which the image (or the media) is uploaded. Use your server url here 
NSURL* requestURL = [NSURL URLWithString:strUrl]; 

// create request 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
[request setHTTPShouldHandleCookies:NO]; 
[request setTimeoutInterval:30]; 
[request setHTTPMethod:@"POST"]; 

// set Content-Type in HTTP header 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant]; 
[request setValue:contentType forHTTPHeaderField: @"Content-Type"]; 

// post body 
NSMutableData *body = [NSMutableData data]; 

FileParamConstant名前はバックエンド側マルチパスイメージフォルダ名ですか?それは私が設定した同じ名前ですが、なぜ動作していないのですか?コードの下NSURLSessionとNSJSONSerializationを使用してObjective Cでマルチパスイメージを投稿する方法は?

答えて

0

完璧に働いている、とそうFileParamConstant画像が保存され、バックエンド側のフォルダの名前です:

NSMutableDictionary * dictParam = [[NSMutableDictionary alloc]init]; 

    [dictParam setObject:[dlf objectForKey:KEY_LOGIN_USER_ID] forKey:@"userId"]; 
    [dictParam setObject:FirstNametextField.text forKey:@"first_name"]; 
    [dictParam setObject:LastNametextField.text forKey:@"last_name"]; 
    [dictParam setObject:MobileNumbertextField.text forKey:@"contact_no"]; 
    [dictParam setObject:AddressTetField.text forKey:@"address"]; 
    [dictParam setObject:photoselect forKey:@"is_upload"]; 


NSString *strUrl=[NSString stringWithFormat:@"%@update_worker_profile?",WEB_SERVICE_URL]; 

// the boundary string : a random string, that will not repeat in post data, to separate post data fields. 
NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy"; 

// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ 
NSString* FileParamConstant = @"picture"; 

// the server url to which the image (or the media) is uploaded. Use your server url here 
NSURL* requestURL = [NSURL URLWithString:strUrl]; 

// create request 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
[request setHTTPShouldHandleCookies:NO]; 
[request setTimeoutInterval:30]; 
[request setHTTPMethod:@"POST"]; 

// set Content-Type in HTTP header 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant]; 
[request setValue:contentType forHTTPHeaderField: @"Content-Type"]; 

// post body 
NSMutableData *body = [NSMutableData data]; 

// add params (all params are strings) 
for (NSString *param in dictParam) { 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [dictParam objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]]; 
} 

CGSize newSize = CGSizeMake(200.0f, 200.0f); 
UIGraphicsBeginImageContext(newSize); 
[Workerprofileimage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0); 

if (imageData) { 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:imageData]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
} 
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]]; 

// setting the body of the post to the reqeust 
[request setHTTPBody:body]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [body length]]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

[request setURL:requestURL]; 

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 

[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 

    NSMutableDictionary *dataResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; 
    NSLog(@"dataResponds=%@",dataResponse); 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     if ([[dataResponse objectForKey:@"status"] isEqualToString:@"success"]) { 

     }else{ 
      NSLog(@"no"); 
     } 
    }); 
}] resume]; 

} 
関連する問題